Clemen Canaria
Clemen Canaria

Reputation: 173

PHP - Merge 2 arrays of object using a key/id

I want to merge the 2 arrays of objects based on the 'id' field of Array1 and the 'itemVendorCode' of Array2. I also wanted to remove from the resulting arrays of object anything that didn't match.

Array1:

Array
(
    [0] => stdClass Object
        (
            [id] => 10-423-1176
            [qty] => 2
            [price] => 12.6
        )

    [1] => stdClass Object
        (
            [id] => 89-575-2354
            [qty] => 24
            [price] => 230.35
        )

    [2] => stdClass Object
        (
            [id] => 89-605-1250
            [qty] => 2
            [price] => 230.35
        )
  )

Array2:

Array
(
    [0] => Item Object
        (
            [internalId] => 14062
            [itemVendorCode] => 89-605-1250
        )

    [1] => Item Object
        (
            [internalId] => 33806
            [itemVendorCode] => 89-575-2354
        )

    [2] => Item Object
        (
            [internalId] => 64126
            [itemVendorCode] => 26-295-1006
        )
)

Upvotes: 1

Views: 1718

Answers (2)

Clemen Canaria
Clemen Canaria

Reputation: 173

I was able to solve this by this code:

$indexed = array();

foreach($itemsArray as $value) {
    $indexed[$value->itemVendorCode] = $value;
}


$results = array();

foreach($vendorItems as $obj) {
    $value = $indexed[$obj->id];

    if (isset($value)) {
        foreach($value as $name => $val) {
            $obj->$name = $val; 
            array_push($results, $obj);
        }
    }

}

print_r($results);

credits to the original poster. I just modified it a bit,

I was able to get the result like this:

Array
(
    [0] => stdClass Object
        (
            [id] => 10-423-1176
            [qty] => 2
            [price] => 12.6
            [internalId] => 2035
            [itemVendorCode] => 10-423-1176
        )

    [1] => stdClass Object
        (
            [id] => 10-423-1176
            [qty] => 2
            [price] => 12.6
            [internalId] => 2035
            [itemVendorCode] => 10-423-1176
        )

    [2] => stdClass Object
        (
            [id] => 14-102-1010
            [qty] => 16
            [price] => 3.2
            [internalId] => 57033
            [itemVendorCode] => 14-102-1010
        )
)

Upvotes: 1

everytimeicob
everytimeicob

Reputation: 327

I think you will have to use array_map function which provides you a callback function to execute on array(s).

In the callback function: - declare your array1 - foreach the second array - set an if statement to check that the current iteration with the id value matches the itemVendorCode of the array2 and return it

something like this:

// You have to specify to PHP to use a local copy of your $array2 to works with it into your callback
$cb = function ($obj1) use ($array2)
{
    // you foreach this array
    foreach ($array2 as $obj2) {
        // if the value of id matches itemVendorCode
        if ($obj1->id === $obj2->itemVendorCode) {
            // you return the id
            return $obj->id;
        }
    }
};

// this function will fill a new array with all returned data
$mergedArray = array_map($cb, $array1);

This code is a sample but doesn't provide you, your needled solution, try to update it to do what you exactly want ;)

Upvotes: 0

Related Questions