Stack Exchange
Merge two stdClass object array in php
I have the following two stdClass array from a loop. Now I need to merge them when when the key 'id' matches.
array (size=1)
  0 => 
    object(stdClass)[28]
      public 'id' => string '78' (length=2)
      public 'quantity' => string '5' (length=1)




array (size=1)
  1 => 
    object(stdClass)[31]
      public 'product_id' => string '78' (length=2)
      public 'quantity' => string '1' (length=1)

So the final array become 
array (size=1)
  1 => 
    object(stdClass)[31]
      public 'product_id' => string '78' (length=2)
      public 'quantity' => string '6' (length=1)

Any help on how to do that ? I decode the original data using json_decode from [{"id":"78","quantity":"1"}] this format of data.

Is array1 and array2 a database resultset? Have you tried to loop the first array and map the second array?

Yes, They are database resultset. they are basically serialized string in database which when pulled are json_decode. They are from same loop.

If you add an extra parameter to the json_decode, you can get the data as an associative array, which is much easier to work with.  I've done a couple of versions ( the first is PHP 7 ), pick one which works with your system.
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );

$arr1 = json_decode('[{"id":"78","quantity":"1"}, {"id":"79","quantity":"3"}]', true);
$arr2 = json_decode('[{"id":"78","quantity":"5"}]', true);

$arr3 = array_merge($arr1, $arr2);

// V7
$result = [];
foreach ( $arr3 as $element )   {
    $result[$element['id']] = ($result[$element['id']]??0)
              + $element['quantity'];
}

print_r($result);
// Prior to V7
$result = array();
foreach ( $arr3 as $element )   {
    if ( !isset($result[$element['id']]) ){
        $result[$element['id']] = 0;
    }
    $result[$element['id']] += $element['quantity'];
}

print_r($result);

I've added another element to show how this adds up, but the output of this is...
Array
(
    [78] => 6
    [79] => 3
)
Array
(
    [78] => 6
    [79] => 3
)


Perfect solution. Thanks !

Here is a solution that maintains the format in your original question.
It also uses array_reduce which is a concise way to process an array.
<?php
$input1 = '[{"id":"78","quantity":"7800"},
    {"id":"79","quantity":"7900"},
    {"id":"80","quantity":"8000"}]';
$input2 = '[{"id":"78","quantity":"6"},
    {"id":"79","quantity":"8"},
    {"id":"80","quantity":"6"},
    {"id":"81","quantity":"7"}]';

$input1Arr = json_decode($input1);
$input2Arr = json_decode($input2);

$combinedArrays = array_merge($input1Arr, $input2Arr);

echo "combinedArrays = " . print_r($combinedArrays, true) . "\n";

$result = array_reduce($combinedArrays, 
    function($intermediateResult, $item){
        if ( ! array_key_exists($item->id, $intermediateResult) ) {
            // First time encountering an object with this id
            $intermediateResult[$item->id] = $item;
        }
        else {
            // We have an object with this id already so just add the quantity
            $intermediateResult[$item->id]->quantity += $item->quantity;
        }
        return $intermediateResult;
    }, []);
// Get the values from the result array
print_r(array_values($result));


Worked like a charm. Thanks !
