您的位置:首页 > 编程语言 > PHP开发

php5.5新函数array_column

2014-12-28 09:56 621 查看
php5.5新增了一个新的数组函数,感觉挺使用的,低版本的实现按照如下实现

if(!function_exists('array_column')){
function array_column($input, $columnKey, $indexKey=null){
$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, $columnKey, 1);
$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, $indexKey, 1);
$key        = (is_array($key) && !empty($key)) ? current($key) : null;
$key        = is_null($key) ? 0 : $key;
}else{
$key        = isset($row[$indexKey]) ? $row[$indexKey] : 0;
}
}
$result[$key]       = $tmp;
}
return $result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: