您可以通过 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
循环的另一种方法:
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
)