语法
array_column(array $array, $column, $index_key = null)
示例
<?php $list = [ [ 'id' => 1, 'name' => 'Tom', 'age' => 23 ], [ 'id' => 2, 'name' => 'Jack', 'age' => 24 ] ]; // 从2维数组中取name列为值 print_r(array_column($list, 'name')); // 从2维数组中取name列为值,id列为键 print_r(array_column($list, 'name', 'id')); // 从2维数组中取整个元素为值,id列为键 print_r(array_column($list, null, 'id'));
输出结果
Array ( [0] => Tom [1] => Jack ) Array ( [1] => Tom [2] => Jack ) Array ( [1] => Array ( [id] => 1 [name] => Tom [age] => 23 ) [2] => Array ( [id] => 2 [name] => Jack [age] => 24 ) )