PHP
參考地址
if(!function_exists(
function array_column($input
$columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
$indexKeyIsNull = (is_null($indexKey)) ? true : false;
$indexKeyIsNumber = (is_numeric($indexKey)) ? true : false;
$result = array();
foreach((array)$input as $key=>$row){
if($columnKeyIsNumber){
$tmp = array_slice($row
$tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
}else{
$tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
}
if(!$indexKeyIsNull){
if($indexKeyIsNumber){
$key = array_slice($row
$key = (is_array($key) && !empty($key)) ? current($key) : null;
$key = is_null($key) ?
}else{
$key = isset($row[$indexKey]) ? $row[$indexKey] :
}
}
$result[$key] = $tmp;
}
return $result;
}
}
// 使用例子
$records = array(
array(
)
array(
)
array(
)
array(
)
);
$firstNames = array_column($records
print_r($firstNames);
/*
Array
(
[
[
[
[
)
*/
$records = array(
array(
array(
array(
);
$lastNames = array_column($records
print_r($lastNames);
/*
Array
(
[
[
[
)
*/
$mismatchedColumns = array(
array(
)
array(
)
array(
)
);
$foo = array_column($mismatchedColumns
print_r($foo);
/*
Array
(
[bar] => foo
[
[garply] => grault
)
*/
array_column 用於獲取二維數組中的元素(PHP
<?php
// Array representing a possible record set returned from a database
$records = array(
array(
)
array(
)
array(
)
array(
)
);
$first_names = array_column($records
print_r($first_names);
?>
Array
(
[
[
[
[
)<?php
// Using the $records array from Example #
$last_names = array_column($records
print_r($last_names);
?>
Array
(
[
[
[
[
)
From:http://tw.wingwit.com/Article/program/PHP/201311/20974.html