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

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

Example

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

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

[2] =>
    [0] => 3

[3] =>
    [0] => 3

therefore, order 0 can be executed by employee 0.1 or 2. order 1. orders 2 and 3 can only be executed by employee 3.

I need to return a bool that is true if each order has one unique employee to execute it. therefore, in this case, return false, because only one employee is available to fulfill orders 2 and 3 and cannot be assigned to both.

Hope this makes sense. pressing it on my agh phone

+4
3

php-, , , , , , .

<?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);
?>

, compare ($ your_array_name), 0 (false) 1 (true).
, .

Edit:
, - , .
, true, .

.
= > .
= > , , , .
= > , , , .
, n , n , .
n = , , true, .

, . , .

+4

array_reduce array_diff .

//Each step tries to add another employee id to the $carry array
function calc($carry, $item) 
{
    if (!$carry) {
        $carry = array();
    }
    $diff = array_diff($item, $carry);
    if (count($diff) > 0) {
        array_push($carry, reset($diff));
    }
    return $carry;
}

function cmp($a, $b)
{
    return count($a) - count($b);
}

function checkCondition($arrayToCheck) 
{
    uasort($arrayToCheck, 'cmp');
    $reduced = array_reduce($arrayToCheck, "calc");

    //If we have a shorter array than the number of orders, we have a problem
    return count($reduced) == count($arrayToCheck);
}

$o = array(
    array(0,1,2),
    array(0,1,2),
    array(3),
    array(3),
);

$result = checkCondition($o);

echo $result + " " + $result ? "Good" : "Bad";  //Should be Bad

$o = array(
    array(0,1,2),
    array(0,1,2),
    array(1),
    array(3),
);

$result = checkCondition($o);

echo $result + " " + $result ? "Good" : "Bad";  //Should be Good

"" , .

+2

. , .

,

:

, ,

  • node ,
  • node - ( )

, true. false.


EDIT: J. . , .

, :

  • orders , true
  • - , false
  • Get the e employee with the fewest orders
  • Get e order o with the least number of employees
  • Remove e and o from orders
  • Repeat with 1.

JavaScript injection

function areAllOrdersFulfilled(orders) {
    while (orders.length !== 0) {
        if (undefined !== orders.find( o => o.length === 0))
            // An order has no employee to fulfill it
            return false;
        // Get employee and order to remove
        let employees = [];
        orders.forEach( order => {
            order.forEach( employee => {
                employees[employee] = (employees[employee] || 0) + 1;
            });
        });
        let employeeToRemove = employees.indexOf(employees.slice().sort()[0]), // Employee with less orders
            orderToRemove = orders
                .sort( (o1, o2) => o1.length - o2.length ) // In-place sort orders
                .findIndex( o => o.includes(employeeToRemove)); // Order with less employees
        // Remove
        orders.splice(orderToRemove,1);
        orders = orders.map( o => o.filter(e => e !== employeeToRemove) )
    }
    return true;
}

You can run it as follows:

const orders = [
    [0,1,2],
    [0,1,2],
    [3],
    [3]
];

console.log(areAllOrdersFulfilled(orders));
+2
source

Source: https://habr.com/ru/post/1693288/


All Articles