php5.5新數(shù)組函數(shù)array_column使用_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:PHP preg_match的匹配多國語言的技巧這篇文章主要是介紹了PHP preg_match中匹配多國語言的方法,需要的朋友可以參考下 正則:[\S]{2,32} 過濾是管用的 PHP中: 復(fù)制代碼 代碼如下: ?php var_dump( preg_match(/[\S\b]{2,32}/,'') ); echo 'hr /'; var_dump( preg_match(/[\S\b]{2,32}/,'中國') ); 是不行的
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
)
*/
分享:php 中序列化和json使用介紹【序列化的概念】 序列化是將對象狀態(tài)轉(zhuǎn)換為可保持或可傳輸?shù)母袷降倪^程。與序列化相對的是反序列化,它將流轉(zhuǎn)換為對象。這兩個(gè)過程結(jié)合起來,可以輕松地存儲和傳輸數(shù)據(jù)。 將對象的狀態(tài)信息轉(zhuǎn)換為可以存儲或傳輸?shù)拇绑w的過程。 在序列化期間,對象將其當(dāng)前狀態(tài)寫入到臨
相關(guān)PHP教程:
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 解析將多維數(shù)組轉(zhuǎn)換為支持curl提交的一維數(shù)組格式
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- 《PHP設(shè)計(jì)模式介紹》第五章 注冊模式
- 如何成為PHP高手
- php4和php5單態(tài)模式(Singleton Pattern)寫法
- 揭秘PHP+MySQL動(dòng)態(tài)網(wǎng)站開發(fā)基礎(chǔ)實(shí)例教程
- 基于PHP選項(xiàng)與信息函數(shù)的使用詳解
- PHP register_shutdown_function函數(shù)的深入解析
- 利用PHP的OOP特性實(shí)現(xiàn)數(shù)據(jù)保護(hù)
- PHP技巧:用PHP導(dǎo)出MySQL數(shù)據(jù)庫內(nèi)容為.sql文件
- 基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法詳解
- PHP進(jìn)階技巧:php用流方式制作縮略圖
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php5.5新數(shù)組函數(shù)array_column使用。