数组合并不合并一个元素

时间:2015-01-02 12:36:22

标签: php arrays array-merge

数组1 $gpsArr

Array
(
    [store_id] => Array
        (
            [0] => 101
        )

    [store_name] => Array
        (
            [0] => Out of the Blue - Powai
        )

    [store_logo] => Array
        (
            [0] => /Out-of-the-Blue-Powai.jpg
        )

    [deals_image] => Array
        (
            [0] => 
        )

)

数组2 $dataArr

Array
(
    [store_id] => Array
        (
            [0] => 87
            [1] => 88
            [2] => 99
            [3] => 100
        )

    [store_name] => Array
        (
            [0] => The Barking Deer Brewpub
            [1] => The Den
            [2] => Mini Punjab's Chinese Court
            [3] => Out of the Blue - Khar
        )

    [store_logo] => Array
        (
            [0] => /The-barking-deer-brewpub.png
            [1] => /the-den.png
            [2] => /Mini-Punjabs-Chinese-Court-Bandra.png
            [3] => /Out-of-the-Blue-Khar.jpg
        )

    [deals_image] => Array
        (
            [0] => the-barking-deer-brewpub-thumb297.jpg
            [1] => the-den-thumb297.jpg
            [2] => mini-punjub-chinese-court-thumb297.jpg
            [3] => out-of-the-blue-Khar-thumb297.jpg
        )

)

使用以下代码合并上述两个数组:

$result = array_unique(array_merge($gpsArr,$dataArr), SORT_REGULAR);

返回以下结果:

Array
(
    [store_id] => Array
        (
            [0] => 87
            [1] => 88
            [2] => 99
            [3] => 100
        )

    [store_name] => Array
        (
            [0] => The Barking Deer Brewpub
            [1] => The Den
            [2] => Mini Punjab's Chinese Court
            [3] => Out of the Blue - Khar
        )

    [store_logo] => Array
        (
            [0] => /The-barking-deer-brewpub.png
            [1] => /the-den.png
            [2] => /Mini-Punjabs-Chinese-Court-Bandra.png
            [3] => /Out-of-the-Blue-Khar.jpg
        )

    [deals_image] => Array
        (
            [0] => the-barking-deer-brewpub-thumb297.jpg
            [1] => the-den-thumb297.jpg
            [2] => mini-punjub-chinese-court-thumb297.jpg
            [3] => out-of-the-blue-Khar-thumb297.jpg
        )

)

如您所见,合并数组后未显示store_id = 101。为什么会这样?我觉得我做了些傻事,但是你能告诉我我做错了吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试使用array_merge_recursive()