php 5.5使用 array_column的方法
阅读原文时间:2023年09月29日阅读:1
  
php 5.5使用 array\_column的方法  
  
public 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;  
}