array_column的用法
array_column(array, array_key, [index_key])
array_column用在从数据库中返回的数据集,为的是得到某一列的数据
用例如下:
1.不规定返数据的键值
$a = array(
array(
'id' => 5698,
'first_name' => 'Bill',
'last_name' => 'Gates',
),
array(
'id' => 4767,
'first_name' => 'Steve',
'last_name' => 'Jobs',
),
array(
'id' => 3809,
'first_name' => 'Mark',
'last_name' => 'Zuckerberg',
)
);
$id = array_column($a, 'id');
print_r($id);
返回的值为:Array ( [0] => 5698 [1] => 4767 [2] => 3809 )
2.规定返回数组的键值
$a = array(
array( 'id' => 5698, 'first_name' => 'Bill', 'last_name' => 'Gates', ),
array( 'id' => 4767, 'first_name' => 'Steve', 'last_name' => 'Jobs', ),
array( 'id' => 3809, 'first_name' => 'Mark', 'last_name' => 'Zuckerberg', )
);
$last_names =array_column($a, 'last_name', 'id');
print_r($last_names);
返回的值为:
Array
(
[5698] => Gates
[4767] => Jobs
[3809] => Zuckerberg
)