简体   繁体   English

如何将数组中的数组合并到单个数组

[英]How to merge array within array to single array

I am trying to merge the associative array to single array but no success . 我试图将关联数组合并到单个数组,但没有成功。

Array
(
    [0] => Array
        (
            [313] => 9513
            [361] => 2280
            [68] => 9513
            [363] => 1591
            [314] => 1610
            [355] => 1908
            [357] => 1445
            [389] => 290
            [358] => 203
            [388] => 157
            [360] => 144
            [362] => 105
            [383] => 28
        )

    [1] => Array
        (
            [385] => 5619
            [4] => 25090
            [45] => 25090
            [321] => 6041
            [323] => 3208
            [324] => 2686
            [317] => 2474
            [327] => 1868
            [386] => 1680
            [310] => 1698
            [312] => 539
            [320] => 406
            [390] => 369
            [326] => 331
            [315] => 285
            [325] => 219
            [356] => 226
            [318] => 213
            [311] => 138
            [319] => 110
            [382] => 61
        )

    [2] => Array
        (
            [4] => 54098
            [5] => 54098
            [8] => 30669
            [18] => 4880
            [74] => 3457
            [17] => 2702
            [21] => 2217
            [14] => 2216
            [73] => 2109
            [7] => 10085
            [66] => 1969
            [20] => 1481
            [13] => 1234
            [64] => 1180
            [11] => 1097
            [75] => 727
            [15] => 592
            [67] => 767
            [10] => 457
            [391] => 352
            [387] => 356
        )
)

The expected output is :- 预期的输出是:-

Array
(
[0] => Array
    (
        [313] => 9513
        [361] => 2280
        [68] => 9513
        [363] => 1591
        [314] => 1610
        [355] => 1908
        [357] => 1445
        [389] => 290
        [358] => 203
        [388] => 157
        [360] => 144
        [362] => 105
        [383] => 28
        [385] => 5619
        [4] => 25090
        [45] => 25090
        [321] => 6041
        [323] => 3208
        [324] => 2686
        [317] => 2474
        [327] => 1868
        [386] => 1680
        [310] => 1698
        [312] => 539
        [320] => 406
        [390] => 369
        [326] => 331
        [315] => 285
        [325] => 219
        [356] => 226
        [318] => 213
        [311] => 138
        [319] => 110
        [382] => 61
        [4] => 54098
        [5] => 54098
        [8] => 30669
        [18] => 4880
        [74] => 3457
        [17] => 2702
        [21] => 2217
        [14] => 2216
        [73] => 2109
        [7] => 10085
        [66] => 1969
        [20] => 1481
        [13] => 1234
        [64] => 1180
        [11] => 1097
        [75] => 727
        [15] => 592
        [67] => 767
        [10] => 457
        [391] => 352
        [387] => 356
    )
 )

If you don't know the number of elements, the obvious way would be to do it in a loop: 如果您不知道元素的数量,那么显而易见的方法是循环执行:

$newArray = array();
foreach ($multiarray as $array){
    $newArray = array_merge($newArray,array_values($array));
}

// $newArray contains your flattened array.

Note however that you cannot have multiple array elements with the same key - it's not possible. 但是请注意,不能有多个具有相同键的数组元素-这是不可能的。 I've set it up to discard the array keys and just keep the values - if you want to keep the keys (but bear in mind that a 'newer' key will override an older key, then remove the call to array_values() 我已将其设置为丢弃数组键并仅保留值-如果您想保留键(但请记住,“更新”键将覆盖旧键,然后删除对array_values()的调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM