0

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?

4
  • Are the two arrays in the same order? Why not just match $orderDetails[$i] with $fulfillmentDetails[$i]? Commented Jan 8, 2018 at 21:53
  • @Barmar good question, they are not in any particular order. I am essentially looking for a match in an unordered structure Commented Jan 8, 2018 at 21:53
  • If they're unordered, how do you know which fulfillment goes with which order? Can you use any fulfillment where faceValue matches denomination? Commented Jan 8, 2018 at 21:55
  • Removing the item from the fulfillment array seems like the simplest method. Commented Jan 8, 2018 at 21:55

1 Answer 1

1

This loop seems to be incorrect:

// 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)

}

You add $o to the $newObj array and then add a new item to the array called pinID, instead of adding the pinID to the object. You should fix it like this:

// Loop over the order details
foreach($orderDetails->result() as $o){

    // Find a pin in the fulfilment details that matches what we need
    $o['pinID'] = get_pin_from_inventory($fulfilmentDetails, $o->faceValue);
    $newObj[] = $o;

}

Now, a further step is to make sure it is not duplicated:

// Loop over the order details
foreach($orderDetails->result() as $o){

    $found = false;
    $pinID = get_pin_from_inventory($fulfilmentDetails, $o->faceValue);
    foreach ($newObj as $temp) {
        //If it is a duplicate, then $found = true
        //To check whether it is a duplicate compare its pinID to $pinID
        //and do any further checks if needed
    }
    if (!$found) {
        // Find a pin in the fulfilment details that matches what we need
        $o['pinID'] = $pinID;
        $newObj[] = $o;
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

I am a little confused at this part - "If it is a duplicate, then $found = true". However, below you say if($found) then its a match. Wouldn't it need to be if(!$found) as in there was no duplicate?
@SBB you are right, that was a mistake. I have edited my answer to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.