Explanation:
Using PHP, I have a form that lets users create outgoing orders.
The user is able to select what inventory item they would like to send in a specific box # to a specific customer.
I would like to add validation to this form because the user should not be able to select 2 different customers for the same box #.
Example:
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.
When the form is submitted I am creating an array like this:
$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"
)
);
Question:
How do I go about iterating through this array to validate that every person has their own Box #?
This is what I am trying but I am getting stuck:
$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']);
}
}