自己寫的兼容低于PHP 5.5版本的array_column()函數(shù)
更新時間:2014年10月24日 10:47:58 投稿:junjie
這篇文章主要介紹了自己寫的兼容低于PHP 5.5版本的array_column()函數(shù),array_column是PHP 5.5新增函數(shù),有時在低版本中也可能要用到,需要的朋友可以參考下
array_column 用于獲取二維數(shù)組中的元素(PHP 5.5新增函數(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; } }
您可能感興趣的文章:
- php5.5新數(shù)組函數(shù)array_column使用
- PHP中array_map與array_column之間的關(guān)系分析
- php array_flip() 刪除數(shù)組重復(fù)元素
- php數(shù)組函數(shù)序列之a(chǎn)rray_unique() - 去除數(shù)組中重復(fù)的元素值
- PHP合并數(shù)組+與array_merge的區(qū)別分析
- php數(shù)組函數(shù)序列之a(chǎn)rray_key_exists() - 查找數(shù)組鍵名是否存在
- php數(shù)組函數(shù)序列之a(chǎn)rray_keys() - 獲取數(shù)組鍵名
- php array_filter除去數(shù)組中的空字符元素
- PHP中unset,array_splice刪除數(shù)組中元素的區(qū)別
- php提示W(wǎng)arning:mysql_fetch_array() expects的解決方法
- php中array_column函數(shù)簡單實現(xiàn)方法
相關(guān)文章
php中模擬POST傳遞數(shù)據(jù)的兩種方法分享
php中模擬POST傳遞數(shù)據(jù)的兩種方法分享,有時候需要模擬提交數(shù)據(jù)方便采集,需要的朋友可以參考下。2011-09-09使用GROUP BY的時候如何統(tǒng)計記錄條數(shù) COUNT(*) DISTINCT
在有g(shù)roup by的時候,如何統(tǒng)計結(jié)果記錄的數(shù)量?需要的朋友可以參考下。2011-04-04vs中通過剪切板循環(huán)來循環(huán)粘貼不同內(nèi)容
只要按Ctrl+Shift+V鍵,就可以在過去剪切或復(fù)制到剪切板上的20條內(nèi)容間進行循環(huán)粘貼,太帥了哈哈!2011-04-04