PHP是否具有可以对布尔结果进行排序/赋值的内置数组函数? [英] does php have built-in array functions that can sort/assign values for boolean result?

查看:99
本文介绍了PHP是否具有可以对布尔结果进行排序/赋值的内置数组函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订单数组,键代表订单号.每个元素包含一组员工,这些员工可以履行由员工编号表示的那些订单.

示例

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

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

[2] =>
    [0] => 3

[3] =>
    [0] => 3

因此,订单0可由员工0,1或2履行. 以及1. 订单2和3只能由员工3履行.

我需要返回布尔值,如果每个订单都有一位唯一的员工来履行它,则为true.因此在这种情况下返回false,因为只有一名员工可以执行订单2和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(假)或1(真).
希望对您有所帮助.


我怀疑您会在其他任何地方找到此代码,因为我是为这个问题编写的.
我一直在做证明,可以证明当函数返回true时,它就是true.

该函数返回true.
=>订单数组为空.
=>在此之前,orders数组包含一个至少可以由一名员工完成的订单.
=>在此之前,orders数组包含两个可以分别由至少一个员工和两个员工分别完成的订单.
通过重复执行,我们可以推断出结束前有n步,有n笔订单,所有订单都可以由至少一名唯一的雇员执行.
如果n =初始数组的大小,我们可以得出结论,如果函数返回true,则是正确的.

如果该证明不正确,请告诉我.如果下半年找到证据,我将再次编辑我的帖子.

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

解决方案

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.

这篇关于PHP是否具有可以对布尔结果进行排序/赋值的内置数组函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆