layout: post
title: "兼容低于PHP5.5的array_column函数"
date: 2016-08-05 23:13:46 +0800
comments: true
categories: [php]
array_column 用于获取二维数组中的元素(PHP 5.5新增函数),但我们有时候需要在低版本的PHP环境中使用…
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