我需要合并2个具有以下结构的数组: $ primary_array
Array
(
[0] => stdClass Object
(
[name] => Gold
[price] => 150.00
)
[1] => stdClass Object
(
[name] => Silver
[price] => 75.00
)
)
$ secondary_array
Array
(
[0] => stdClass Object
(
[name] => Gold
[price] => 100.00
)
[1] => stdClass Object
(
[name] => Silver
[price] => 75.00
)
[2] => stdClass Object
(
[name] => Bronze
[price] => 50.00
)
)
预期结果:
Array
(
[0] => stdClass Object
(
[name] => Gold
[price] => 150.00
)
[1] => stdClass Object
(
[name] => Silver
[price] => 75.00
)
[2] => stdClass Object
(
[name] => Bronze
[price] => 50.00
)
)
Pricefrom主数组必须接管辅助数组,但要填充辅助数组中缺少的数据。
我尝试使用
array_unique(array_merge($primary_array,$secondary_array), SORT_REGULAR)
但它没有产生预期的结果
答案 0 :(得分:1)
你有几个选择。
退房 array_replace_recursive http://php.net/manual/en/function.array-replace-recursive.php
array_merge_recursive http://php.net/manual/en/function.array-merge-recursive.php