Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does php have built-in array functions that can sort/assign values for boolean result?

I have an array of orders, key represents order#. each element contains an array of employees that can fulfill those orders represented by employee number.

example

[0] =>         // <-- order#
    [0] => 0,
    [1] => 1,
    [2] => 2

[1] =>
    [0] => 0,
    [1] => 1,
    [2] => 2

[2] =>
    [0] => 3

[3] =>
    [0] => 3

so order 0 can be fulfilled by employee 0,1, or 2. order 1 as well. orders 2 and 3 can only be fulfilled by employee 3.

i need to return bool which is true if each order has one unique employee to fulfill it. so in this case return false because only one employee is available to fulfill orders 2 and 3 and cant be assigned to both.

i hope that makes sense. tapping this out on my phone agh

like image 523
Brian Patterson Avatar asked Jan 29 '23 17:01

Brian Patterson


1 Answers

This is a php function I wrote quickly that does what you want, I have quickly tested it and it seems to work properly.

<?php

function compare($orders){

    if(sizeof($orders) == 0){
        return 1;
    }
    $quantity = array();
    foreach ($orders as $order) {
        if(sizeof($order) == 0){
            return 0;
        }
        foreach ($order as $employee) {
            if(array_key_exists($employee, $quantity)){
                $quantity[$employee]++;
            }
            else{
                $quantity[$employee] = 1;
            }
        }
    }
    $chosenEmployees = array_keys($quantity, min($quantity));
    $chosenEmployee = $chosenEmployees[0];

    $length = array();
    foreach ($orders as $key => $order) {
        $length[$key] = sizeof($order);
    }
    for ($i=0; $i < sizeof($orders); $i++) {
        $chosenOrders = array_keys($length, min($length));
        foreach ($chosenOrders as $orderKey) {
            if(in_array($chosenEmployee, $orders[$orderKey])){
                unset($orders[$orderKey]);
                foreach ($orders as $key1 => $order) {
                    foreach ($order as $key2 => $employee) {
                        if($employee == $chosenEmployee){
                            unset($orders[$key1][$key2]);
                        }           

                    }
                }
                return compare($orders);
            }
            else{
                unset($length[$orderKey]);
            }
        }
    }

}
$out = compare($orders);
?>

To use it, type compare($your_array_name), and the function will return 0 (false) or 1 (true).
Hope it helps.

Edit:
I doubt you'll find this code anywhere else because I wrote it for this question.
I have been working on proof and can prove that when the function returns true, it is true.

The function returned true.
=> The orders array is empty.
=> Before that, the orders array contained one order that can be done by at least employee one.
=> Before that, the orders array contained two orders that can be done respectively by at least employee one and employee two.
Via recurrence, we can deduce that n steps before the end, there were n orders, all doeable by at least one unique employee.
If n = size of initial array, we can conclude that if the function returns true, it is correct.

If this proof is not correct, please let me know. I will edit my post again if I find proof for the second half.

like image 115
J. Quick Avatar answered Jan 31 '23 07:01

J. Quick