I have two arrays of data that I am working with.
- Array 1 is objects of Order Details that a customer has submitted.
- Array 2 is the fulfillment details, the items that have been purchased.
I need to find their item that they purchased in the fulfillment details and get a value from it so I can store it with the order details.
The one important aspect of this is duplication. An order detail record can't use the same order item (pinID) more than once.
We will assume here that both objects are of the same size and contain the appropriate data for the match.
Example:
Array 1 - Order Details
stdClass Object
(
[customerID] => 8
[customerProductID] => 118
[productID] => 2
[faceValue] => 25
)
stdClass Object
(
[customerID] => 15
[customerProductID] => 119
[productID] => 2
[faceValue] => 25
)
stdClass Object
(
[customerID] => 14
[customerProductID] => 120
[productID] => 7
[faceValue] => 50
)
stdClass Object
(
[customerID] => 18
[customerProductID] => 121
[productID] => 7
[faceValue] => 50
)
Array 2 - Order Fulfillment details
stdClass Object
(
[pinID] => 75
[denomination] => 25
)
stdClass Object
(
[pinID] => 76
[denomination] => 25
)
stdClass Object
(
[pinID] => 77
[denomination] => 50
)
stdClass Object
(
[pinID] => 78
[denomination] => 50
)
Pseudo Code:
// New object to hold all the updated data
$newObj = [];
// Loop over the order details
foreach($orderDetails->result() as $o){
// Find a pin in the fulfilment details that matches what we need
$newObj[] = $o;
$newObj['pinID'] = get_pin_from_inventory($fulfilmentDetails, $o->faceValue)
}
// Find a matching item in the fulfillment details
function get_pin_from_inventory($fulfilmentDetails, $orderItem){
// Loop over each fulfilment item
foreach($pins->result() as $p){
// If we found a pin that matches our item, return it.
// It is important that this pinID has not already been assigned to another order in the $newObj['pinID']
if($p->denomination == $orderItem){
return $p->pinID;
}
}
}
End Result of $newObj
stdClass Object
(
[customerID] => 8
[customerProductID] => 118
[productID] => 2
[faceValue] => 25
[pinID] => 75
)
stdClass Object
(
[customerID] => 15
[customerProductID] => 119
[productID] => 2
[faceValue] => 25
[pinID] => 76
)
stdClass Object
(
[customerID] => 14
[customerProductID] => 120
[productID] => 7
[faceValue] => 50
[pinID] => 77
)
stdClass Object
(
[customerID] => 18
[customerProductID] => 121
[productID] => 7
[faceValue] => 50
[pinID] => 78
)
How should I go about checking for the duplication when creating the new object? Should I be removing items from the fulfillment array each time to prevent the possibility of grabbing one in use or is there a better way?
$orderDetails[$i]with$fulfillmentDetails[$i]?faceValuematchesdenomination?