php5.5新數(shù)組函數(shù)array_column使用
PHP5.5發(fā)布了,其中增加了一個(gè)新的數(shù)組函數(shù)array_column,感覺不錯(cuò)的!但是低版本PHP要使用,得自己實(shí)現(xiàn):
參考地址:https://wiki.php.net/rfc/array_column
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;
}
}
// 使用例子
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe'
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith'
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones'
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe'
)
);
$firstNames = array_column($records, 'first_name');
print_r($firstNames);
/*
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
*/
$records = array(
array(1, 'John', 'Doe'),
array(2, 'Sally', 'Smith'),
array(3, 'Jane', 'Jones')
);
$lastNames = array_column($records, 2);
print_r($lastNames);
/*
Array
(
[0] => Doe
[1] => Smith
[2] => Jones
)
*/
$mismatchedColumns = array(
array(
'a' => 'foo',
'b' => 'bar',
'e' => 'baz'
),
array(
'a' => 'qux',
'c' => 'quux',
'd' => 'corge'
),
array(
'a' => 'grault',
'b' => 'garply',
'e' => 'waldo'
),
);
$foo = array_column($mismatchedColumns, 'a', 'b');
print_r($foo);
/*
Array
(
[bar] => foo
[0] => qux
[garply] => grault
)
*/
array_column 用于獲取二維數(shù)組中的元素(PHP 5 >= 5.5.0)
<?php
// Array representing a possible record set returned from a database
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$first_names = array_column($records, 'first_name');
print_r($first_names);
?>
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)<?php
// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>
Array
(
[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe
)
相關(guān)文章
PHP5.3的垃圾回收機(jī)制(動(dòng)態(tài)存儲分配方案)深入理解
垃圾回收機(jī)制是一種動(dòng)態(tài)存儲分配方案,它會(huì)自動(dòng)釋放程序不再需要的已分配的內(nèi)存塊,PHP也在語言層實(shí)現(xiàn)了內(nèi)存的動(dòng)態(tài)管理.內(nèi)存的動(dòng)態(tài)管理將開發(fā)人員從繁瑣的內(nèi)存管理中解救出來2012-12-12php定時(shí)計(jì)劃任務(wù)與fsockopen持續(xù)進(jìn)程實(shí)例
本文介紹了php中定時(shí)計(jì)劃任務(wù)的實(shí)現(xiàn)代碼,以及php持續(xù)進(jìn)程fsockopen的用法,需要的朋友可以參考下2014-05-05php class中public,private,protected的區(qū)別以及實(shí)例分析
本篇文章是對php class中public,private,protected的區(qū)別以及實(shí)例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06解析PHP中的file_get_contents獲取遠(yuǎn)程頁面亂碼的問題
本篇文章是對PHP中的file_get_contents獲取遠(yuǎn)程頁面出現(xiàn)亂碼的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06提高PHP性能的編碼技巧以及性能優(yōu)化詳細(xì)解析
include文件時(shí)盡量使用絕對路徑,因?yàn)樗苊饬薖HP去include_path里查找文件的速度,解析操作系統(tǒng)路徑所需的時(shí)間會(huì)更少2013-08-08php實(shí)現(xiàn)的pdo公共類定義與用法示例
這篇文章主要介紹了php實(shí)現(xiàn)的pdo公共類定義與用法,結(jié)合具體實(shí)例形式分析了php實(shí)現(xiàn)的pdo操作類定義及查詢、插入等使用技巧,需要的朋友可以參考下2017-07-07解決Laravel blade模板轉(zhuǎn)義html標(biāo)簽的問題
今天小編就為大家分享一篇解決Laravel blade模板轉(zhuǎn)義html標(biāo)簽的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09PHP使用反射機(jī)制實(shí)現(xiàn)查找類和方法的所在位置
這篇文章主要介紹了PHP使用反射機(jī)制實(shí)現(xiàn)查找類和方法的所在位置,實(shí)例分析了PHP反射機(jī)制的原理與使用反射機(jī)制實(shí)現(xiàn)對類和方法的查找技巧,需要的朋友可以參考下2016-04-04