自己寫的兼容低于PHP 5.5版本的array_column()函數(shù)
更新時(shí)間:2014年10月24日 10:47:58 投稿:junjie
這篇文章主要介紹了自己寫的兼容低于PHP 5.5版本的array_column()函數(shù),array_column是PHP 5.5新增函數(shù),有時(shí)在低版本中也可能要用到,需要的朋友可以參考下
array_column 用于獲取二維數(shù)組中的元素(PHP 5.5新增函數(shù)),但我們有時(shí)候需要在低版本的PHP環(huán)境中使用…
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;
}
}
相關(guān)文章
PHP簡單實(shí)現(xiàn)生成txt文件到指定目錄的方法
這篇文章主要介紹了PHP簡單實(shí)現(xiàn)生成txt文件到指定目錄的方法,簡單對比分析了PHP中fwrite及file_put_contents等函數(shù)的使用方法,需要的朋友可以參考下2016-04-04
zend Framework中的Layout(模塊化得布局)詳解
本篇文章是對zend Framework中的Layout(模塊化得布局)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php安全之直接用$獲取值而不$_GET 字符轉(zhuǎn)義
php安全之直接用$獲取值而不$_GET 字符轉(zhuǎn)義,需要的朋友可以參考下2012-06-06
你應(yīng)該知道PHP浮點(diǎn)數(shù)知識
這篇文章主要介紹了你應(yīng)該知道PHP浮點(diǎn)數(shù)知識,本文講解了PHP浮點(diǎn)數(shù)、PHP數(shù)字的臨界值,精度損失等問題,需要的朋友可以參考下2015-05-05
php實(shí)現(xiàn)通過stomp協(xié)議連接ActiveMQ操作示例
這篇文章主要介紹了php實(shí)現(xiàn)通過stomp協(xié)議連接ActiveMQ操作,結(jié)合實(shí)例形式分析了stomp擴(kuò)展安裝及使用stomp擴(kuò)展連接ActiveMQ具體操作技巧,需要的朋友可以參考下2020-02-02
PHP微信開發(fā)之微信錄音臨時(shí)轉(zhuǎn)永久存儲(chǔ)
這篇文章主要為大家詳細(xì)介紹了PHP微信開發(fā)之微信錄音臨時(shí)轉(zhuǎn)永久存儲(chǔ),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

