【问题标题】:Merging arrays with the same keys same value [duplicate]合并具有相同键相同值的数组[重复]
【发布时间】:2018-12-27 03:13:39
【问题描述】:

数组 1

array(1) {
[0]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplist_name"]=>
        string(18) "PM_Mon_Wed_Fri_Sun"
        ["shoplist_status"]=>
        int(0)
    }
}

数组 2

array(3) {
[2184]=>
        array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplistshop_id"]=>
        int(7)
        ["shoplistshop_status"]=>
        int(0)
}
[2184]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplistshop_id"]=>
        int(8)
        ["shoplistshop_status"]=>
        int(0)
    }
}
[2185]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplistshop_id"]=>
        int(9)
        ["shoplistshop_status"]=>
        int(0)
    }
}

我想要什么

array(2) {
[0]=>
    array(3) {
        ["shoplist_id"]=>
        int(11)
        ["shoplist_name"]=>
        string(18) "PM_Mon_Wed_Fri_Sun"
        ["shoplist_status"]=>
        int(0)
        array(3) {
            [0]=>
                array(2) {
                    ["shoplistshop_id"]=>
                    int(7)
                    ["shoplistshop_status"]=>
                    int(0)
                }
            [1]=>
                array(2) {
                    ["shoplistshop_id"]=>
                    int(8)
                    ["shoplistshop_status"]=>
                    int(0)
                }
            [2]=>
                array(2) {
                    ["shoplistshop_id"]=>
                    int(9)
                    ["shoplistshop_status"]=>
                    int(0)
                }
        }
    }
}

主键是 shoplist_id。

我需要合并数组 1 和数组 2,因为它们具有相同的键和值。

我尝试使用 array_merge / array_merge_recursive 似乎不起作用。

那么如何合并具有相同键和相同值的数组 1 和 2? 那是我想念的吗?非常感谢。

【问题讨论】:

  • 看看this question你的答案会很相似
  • 您从哪里获得阵列 1 和阵列 2?您想要的结果是无效的语法。您的子数组需要有一个键。

标签: php arrays


【解决方案1】:

你可以在这里使用 array_merge:

$expectedOutputArray = array_merge($array1[0], $array2);

如您所见,$array1[0] 本身就是一个数组,因此它会合并并给出预期的输出。

【讨论】:

    猜你喜欢
    • 2014-03-12
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多