2つの方法で達成できます。
1)array_merge
機能の使用
2)+
演算子の使用
以下の例を参照してください。
$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);
出力:
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
)
foreach
ループを使用したもう 1 つの方法:
foreach($obj2 as $k => $v){
$obj1->$k = $v;
}
print_r($obj1);
出力:
stdClass Object
(
[player_id] => 92
[player_name] => Test Name
[player_type_id] => 92
[type] => Test Name
)