1
说明:迭代PHP阵列和验证数据
使用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']);
}
}
因此,如果一个人有一个项目,但使用取箱#该项目应到用户与标注框# ? – ksealey
如果一个盒子已经被使用,验证结束并且警告用户他们不能在同一盒子中使用2个顾客# – osbt