日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php array colum,php5.5新数组函数array_column使用

發布時間:2025/3/8 php 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php array colum,php5.5新数组函数array_column使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

array_column 用于獲取二維數組中的元素(PHP 5 >= 5.5.0),但我們有時候需要在低版本中使用,那么就可以使用下面的代碼即可

PHP5.5發布了,其中增加了一個新的數組函數array_column,感覺不錯的!但是低版本PHP要使用,得自己實現:

參考地址: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 用于獲取二維數組中的元素(PHP 5 >= 5.5.0)

// 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

)

總結

以上是生活随笔為你收集整理的php array colum,php5.5新数组函数array_column使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。