合并关联数组的值php

时间:2014-04-13 16:58:47

标签: php arrays

是否有一个简短的方法(不使用for / foreach)将关联数组值合并为一个,主要是我需要转换此

array(
    (int) 0 => array(
        (int) 0 => '1',
        (int) 1 => '4',
        (int) 2 => '5',
        (int) 3 => '12'
    ),
    (int) 1 => array(
        (int) 0 => '6'
    ),
    (int) 2 => array(
        (int) 0 => '7'
    ),
    (int) 3 => array(
        (int) 0 => '10'
    ),
    (int) 4 => array(
        (int) 0 => '11'
    ),
    (int) 5 => array(
        (int) 0 => '8'
    ),
    (int) 6 => array(
        (int) 0 => '9'
    )
)

到这个

array(
    (int) 0 => '1',
    (int) 1 => '4',
    (int) 2 => '5',
    (int) 3 => '12'
    (int) 4 => '6'  
    (int) 5 => '7'
    (int) 6 => '10'
    (int) 7 => '11'
    (int) 8 => '8'
    (int) 9 => '9'  
)

感谢

1 个答案:

答案 0 :(得分:2)

您可以将call_user_func_array()array_merge()一起用作回调:

$result = call_user_func_array('array_merge', $array);

输出:

Array
(
    [0] => 1
    [1] => 4
    [2] => 5
    [3] => 12
    [4] => 6
    [5] => 7
    [6] => 10
    [7] => 11
    [8] => 8
    [9] => 9
)

Demo