-1

I have the following two arrays of objects:

First Array: $array1

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => John Doe
        )

    [1] => stdClass Object
        (
            [id] => 101
            [name] => Jane Smith
        )

    [2] => stdClass Object
        (
            [id] => 102
            [name] => Richard Tea
        )
)

Second Array: $array2

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [age] => 30
            [other] => [other]
        )

    [1] => stdClass Object
        (
            [id] => 102
            [age] => 26

        )
)

I want to merge these two object arrays and sorted according to id.

Desired output:

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => John Doe
            [age] => 30
            [other] => [other]
        )

    [1] => stdClass Object
        (
            [id] => 101
            [name] => Jane Smith
        )

    [2] => stdClass Object
        (
            [id] => 102
            [name] => Richard Tea
            [age] => 26
        )
)
plcosta
  • 345
  • 4
  • 9
  • 1
    I'm not sure that the linked duplicate is applicable here - the question is asking how to merge an array of objects, specifically `stdClass` objects, not arrays. Additionally, I believe there is an error in the desired output - `[age] => 26` should be a property of array indexed `1` not `2`? – Darragh Enright Dec 16 '15 at 13:07
  • 1
    Exactly @DarraghEnright. The question isn't duplicate, because I asking how to merge an array of objects, specifically stdClass objects, not arrays. It's similar but not duplicate. – plcosta Dec 16 '15 at 13:37
  • No problem - I posted an answer below, let me know if you have any questions. – Darragh Enright Dec 16 '15 at 15:12

2 Answers2

1

You can try it this way with array_merge:

Since your array contents are objects you need to convert them to array when using array_merge.

<?php

$arrayTemp = array();
foreach ($array1 as $key => $value){
    $arrayTemp[] = (object)array_merge((array)$array2[$key], (array)$value);
}

var_dump($arrayTemp);
?>

[Proof of concept]

array(2) {
  [0]=>
  object(stdClass)#5 (3) {
    ["id"]=>
    int(100)
    ["age"]=>
    int(30)
    ["name"]=>
    string(4) "John"
  }
  [1]=>
  object(stdClass)#6 (3) {
    ["id"]=>
    int(101)
    ["age"]=>
    int(40)
    ["name"]=>
    string(4) "jane"
  }
}
Kheshav Sewnundun
  • 1,236
  • 15
  • 37
1

This question is flagged as a duplicate but I do not believe that it's applicable, since the data in this question are two arrays of stdClass() objects.

Edit

My first answer was pretty bone-headed, clearly I had not tested it robustly enough. Thanks to @splash58 who pointed out that I was in fact merging/replacing by array index, not object id. My example only worked because the ordering happened to match in the example data provided. Whoopsy... :)

So instead, again assuming our arrays of data $first and $second we can merge the values of $second into $first with a nested loop and some array/object casting. Finally, we apply a user-defined sort to order the results by id; i.e:

foreach ($first as &$o1) {
    foreach ($second as $o2) {
        if ($o1->id === $o2->id) {
            $o1 = (object) array_merge((array) $o1, (array) $o2);
        }
    }
}

usort($first, function($a, $b) {
    return $a->id >= $b->id;
});

print_r($second);

This yields:

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => John Doe
            [age] => 30
            [other] => other
        )

    [1] => stdClass Object
        (
            [id] => 101
            [name] => Jane Smith
        )

    [2] => stdClass Object
        (
            [id] => 102
            [name] => Richard Tea
            [age] => 26
        )

)

Hope this helps :)

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48