array_column()
从二维数组中获取指定的列的值;
返回这些列值构成的数组;
基础语法:
array array_column(array $array , mixed $column_key [,mixed $index_key = NULL]);
array 返回的数组;
array $array 被操作的数组;
mixed $column_key 列的键名;
-如果column_key = NULL 那么会返回整个二维数组,如果同时 index_key != NULL 那么会把index_key 对应的键值,作为二维数组的第一层键名;
mixed $index_key = NULL 决定返回数组的键名,默认是NULL 返回索引数组;
-如果不是NULL 那么返回数组的键名同 index_key 所对应的列值构成;
语法结构1:
aray array_column(array $array ,mixed $column_key);
返回二维数组列值组成的数组;
实例:
$a_personal_info = array(
'bill' => array(
'first_name' => 'bill_fn',
'last_name' => 'bill_ln',
'age' => 55,
'other' => array('a','b','c')
),
'tom' => array(
'first_name' => 'tom_fn',
'last_name' => 'tom_ln',
'age' => 30,
'other' => array('a','b','c')
),
'jack' => array(
'first_name' => 'jack_fn',
'last_name' => 'jack_ln',
'age' => 20,
'other' => array('a','b','c')
)
);
$a_personal_name = array_column($a_personal_info,'last_name');
print_r($a_personal_name);
#output : Array ( [0] => bill_ln [1] => tom_ln [2] => jack_ln );
返回索引二维数组列值构成的索引数组;
语法结构2:
array array_column(array $array ,mixed $column_key ,mixed $index_key);
返回数组 array(index_key_value => column_key_value);
实例:
$a_personal_info = array(
'bill' => array(
'first_name' => 'bill_fn',
'last_name' => 'bill_ln',
'age' => 55,
'other' => array('a','b','c')
),
'tom' => array(
'first_name' => 'tom_fn',
'last_name' => 'tom_ln',
'age' => 30,
'other' => array('a','b','c')
),
'jack' => array(
'first_name' => 'jack_fn',
'last_name' => 'jack_ln',
'age' => 20,
'other' => array('a','b','c')
)
);
$a_personal_age = array_column($a_personal_info,'age','last_name');
print_r($a_personal_age);
#output : Array ( [bill_ln] => 55 [tom_ln] => 30 [jack_ln] => 20 );
语法结构3:
array array_column(array $array ,NULL,mixed $index_key);
返回整个二维数组,同时修改第一层的键名,用index_key_value 替代;
实例:
<?php
$a_personal_info = array(
0 => array(
'first_name' => 'bill',
'last_name' => 'bill_ln',
'age' => 55,
'other' => array('a','b','c')
),
1 => array(
'first_name' => 'tom',
'last_name' => 'tom_ln',
'age' => 30,
'other' => array('a','b','c')
),
2 => array(
'first_name' => 'jack',
'last_name' => 'jack_ln',
'age' => 20,
'other' => array('a','b','c')
)
);
$a_personal_info_2 = array_column($a_personal_info,NULL,'first_name');
print_r($a_personal_info_2);
/*
output : array(
'bill' => array(
'first_name' => 'bill',
'last_name' => 'bill_ln',
'age' => 55,
'other' => array('a','b','c')
),
'tom' => array(
'first_name' => 'tom',
'last_name' => 'tom_ln',
'age' => 30,
'other' => array('a','b','c')
),
'jack' => array(
'first_name' => 'jack',
'last_name' => 'jack_ln',
'age' => 20,
'other' => array('a','b','c')
)
);
*/
?>
总结:
array array_column(array$array ,mixed $column_key ,mixed $index_key)
返回二维数组列的键值构成的数组 ;可以是索引 or 关联数组;
重新给二维数组的第一层键名命名;