从数组中获取列数据

时间:2017-04-03 16:35:17

标签: php arrays php-7

我有一个数组如下

Array
(
    [0] => Array
        (
            [route_id] => 2/2A
            [direction] => right
            [bus_stop_count] => 1
            [bus_id] => Array
                (
                    [0] => 1000
                    [1] => 1002
                )

        )

    [1] => Array
        (
            [route_id] => 1
            [direction] => right
            [bus_stop_count] => 1
            [bus_id] => Array
                (
                    [0] => 1004
                )

        )

)

我希望获得bus_id的数组,如下所示

Array
(
    [0] => 1000
    [1] => 1002
    [2] => 1004
)

这是我到目前为止所尝试的内容

$bus_ids = array_column($array, 'bus_id');

Array
(
    [0] => Array
        (
            [0] => 1000
            [1] => 1002
        )

    [1] => Array
        (
            [0] => 1004
        )

)

2 个答案:

答案 0 :(得分:4)

这应该有效:

$a = array(...);
call_user_func_array('array_merge', array_column ($a, 'bus_id'))

答案 1 :(得分:0)

您可以使用for循环:

$newArr = Array();
foreach ($arr as $value) $newArr = array_merge($newArr, $value["bus_id"]);