迭代PHP数组并验证数据

时间:2016-11-17 22:28:55

标签: php arrays validation

解释

使用PHP,我有一个表单,允许用户创建传出订单。

用户可以选择他们希望在特定框#中向特定客户发送的库存项目。

我想在此表单中添加验证,因为用户不应该为同一个框#选择2个不同的客户。

示例:

Person A -> Item A -> Box 1
Person A -> Item B -> Box 1
Person B -> Item C -> Box 2
Person B -> Item D -> Box 1 //!! <- This should not be possible because                                    
Person C -> Item E -> Box 3        //Person A is already using Box #1.

提交表单时,我正在创建一个这样的数组:

$data = (object) array
    (
    array (
        "customer" => "Person A",
        "item" => "Item A",
        "box" => "Box 1"
    ),
    array (
        "customer" => "Person A",
        "item" => "Item B",
        "box" => "Box 1"
    ),
    array (
        "customer" => "Person B",
        "item" => "Item C",
        "box" => "Box 2"
    ),
    array (
        "customer" => "Person B",
        "item" => "Item D",
        "box" => "Box 1"
    ),
    array (
        "customer" => "Person C",
        "item" => "Item E",
        "box" => "Box 3"
    )

);

问题:

如何重复遍历此数组以验证每个人都有自己的Box#?

这就是我正在尝试但我陷入困境:

$temp_arr = (object) array();

foreach($data as $row){

    if(!property_exists($temp_arr, $row['customer'])){
      $temp_arr->$row['customer'] = array();
    };

    //Load the boxes into the correct customer array
    if(in_array($row['box'], $temp_arr->$row['customer'])){
        //Duplicate 
    } else {
      array_push($temp_arr->$row['customer'], $row['box']);
    }

}

1 个答案:

答案 0 :(得分:1)

Chrome failed to start: crashed