PHP 二维数组转换为一维数组之array_column()

项目需求:

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

    [1] => Array
        (
            [id] => 7
        )

    [2] => Array
        (
            [id] => 9
        )

    [3] => Array
        (
            [id] => 10
        )

    [4] => Array
        (
            [id] => 12
        )

    [5] => Array
        (
            [id] => 30
        )

    [6] => Array
        (
            [id] => 31
        )

    [7] => Array
        (
            [id] => 32
        )

    [8] => Array
        (
            [id] => 33
        )

    [9] => Array
        (
            [id] => 34
        )

    [10] => Array
        (
            [id] => 45
        )

    [11] => Array
        (
            [id] => 47
        )

    [12] => Array
        (
            [id] => 50
        )

    [13] => Array
        (
            [id] => 51
        )

    [14] => Array
        (
            [id] => 52
        )

    [15] => Array
        (
            [id] => 55
        )

    [16] => Array
        (
            [id] => 76
        )

)

转换成:

Array
(
    [0] => 1
    [1] => 7
    [2] => 9
    [3] => 10
    [4] => 12
    [5] => 30
    [6] => 31
    [7] => 32
    [8] => 33
    [9] => 34
    [10] => 45
    [11] => 47
    [12] => 50
    [13] => 51
    [14] => 52
    [15] => 55
    [16] => 76
)

除去循环,比较有效率的函数:

array_column — 返回数组中指定的一列

只需要array_column(dst_array,column_name)

返回就是column_name的值 组成的一维数组。

 

posted @ 2017-09-13 23:33  -起风了🪐  阅读(2139)  评论(0编辑  收藏  举报