php数组合并,保留第一个数组的值,填补空白

时间:2015-09-14 16:55:02

标签: php arrays

我需要合并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)

但它没有产生预期的结果

1 个答案:

答案 0 :(得分:1)