You can achive in 2 way.
1) Using array_merge
function
2) Using +
operator
Refer below example:
$obj1 = new StdClass();
$obj1->player_id = 92;
$obj1->player_name = 'Test Name';
$obj2 = new StdClass();
$obj2->player_type_id = 92;
$obj2->type = 'Test Name';
$array = array($obj1, $obj2);
$merged_array = (object) ((array) $obj1 + (array) $obj2);
print_r($merged_array);
echo '--------------------------------------- <br />';
$obj_merged = (object) array_merge((array) $obj1, (array) $obj2);
print_r($obj_merged);
Output:
stdClass Object
(
[player_id] => 92
[player_name] => Test Name
[player_type_id] => 92
[type] => Test Name
)
---------------------------------------
stdClass Object
(
[player_id] => 92
[player_name] => Test Name
[player_type_id] => 92
[type] => Test Name
)
One more method using foreach
loop:
foreach($obj2 as $k => $v){
$obj1->$k = $v;
}
print_r($obj1);
Output:
stdClass Object
(
[player_id] => 92
[player_name] => Test Name
[player_type_id] => 92
[type] => Test Name
)