PHP 二维数组转换为一维数组之array_column()
Posted DC Technology Stack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的值 组成的一维数组。
以上是关于PHP 二维数组转换为一维数组之array_column()的主要内容,如果未能解决你的问题,请参考以下文章
PHP二维数组转换成一维数组,一个变量多个二维数组转换成一维数组,PHP二维数组(或任意维数组)转换成一维数组的方法汇总,array_reduce(); array_walk_recursive()