Q:

Merge 2 arrays of objects in PHP

How do you merge array1 and 2?
array1
Array
(
    [0] => stdClass Object
    (
        [name] => bob
        [id] => 84569354306
        [contacts] => Array
        (
            [0] => none
        )
    )
    [1] => stdClass Object
    (
        [name] => jill
        [id] => 456745742
        [contacts] => Array
        (
            [0] => none
        )
    )
)

array2
Array
(
    [0] => stdClass Object
    (
        [name] => bob
        [id] => 84569354306
        [pid] => 1
        [lang] => eng;
        [location] =>
    )
    [1] => stdClass Object
    (
        [name] => jill
        [id] => 456745742
        [pid] => 2
        [lang] => eng;
        [location] =>
    )
)

Result array:
Array
(
    [0] => stdClass Object
    (
        [name] => bob
        [id] => 84569354306
        [pid] => 1
        [lang] => eng;
        [location] =>
        [contacts] => Array
        (
            [0] => none
        )
    )
    [1] => stdClass Object
    (
        [name] => jill
        [id] => 456745742
        [pid] => 2
        [lang] => eng;
        [location] =>
        [contacts] => Array
        (
            [0] => none
        )
    )
)

I've tried an array_merge() which seems to add objects next to each other in the array rather than merging the objects.
I'm pretty sure this question is similar to what I need, but I'm having difficulty with the simple foreach loop.

A:

You can cast the two objects to array and then re-cast back to an object. The general syntax is:
$merged = (object)array_merge_recursive((array)$firstObj, (array)$secondObj);
                              ^
                              | note the recursive in your case

Also, if you are using objects like that maybe you should simply stick to array. It has very little to no sense to do something like that with objects
With multiple items
If you have multiple items you simply need to wrap up my script inside a loop:
function myCustomMerge($array1, $array2) {
   assert('count($array1) == count($array2)');
   $result = array();
   foreach($array1 as $k=>$v) {
    $item = array_merge_recursive((array)$array1[$k], (array)$array2[$k]);
    $result[]=$item; // use (object)$item if you need objects
   }
   return $result;
}

Solution without casting
If you prefer not to cast back and forth between array and object you can use get_object_vars():
$obj2props = get_object_vars($obj2);
foreach ($obj2props as $prop => $value) {
    $obj1->$prop = $value;
}
return $obj;

A:

Another way of doing it:
function merge_values(){
    $list = func_get_args();
    while( count( $list ) > 1 ){
        $array1 = array_shift( $list );
        $array2 = array_shift( $list );

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

        array_unshift( $list, $merged_array );
    } 
    return current( $list );
}

$merged = merge_values( $array1, $array2 );

