Home > php教程 > php手册 > array_column:将一个多维数组的转换为一个键值对数组

array_column:将一个多维数组的转换为一个键值对数组

WBOY
Release: 2016-06-06 19:39:38
Original
1553 people have browsed it

array_column(array$input,$column_key,$index_key) 第一个参数为原始数组,第二个参数(多维数组键名)为新数组的值,第三个参数(多维数组键名)为键.结果为一个键值对数组 $array = [['id' = 1,'name' = 'tom','age' = 18],['id' = 2,'name' = 'jack','age' = 1

array_column( array $input , $column_key , $index_key)
第一个参数为原始数组,第二个参数(多维数组键名)为新数组的值,第三个参数(多维数组键名)为键.结果为一个键值对数组

$array = [
	['id' => 1,'name' => 'tom','age' => 18],
	['id' => 2,'name' => 'jack','age' => 19],
	['id' => 3,'name' => 'mick','age' => 20],
	['id' => 4,'name' => 'lucy','age' => 21],
	['id' => 5,'name' => 'bill','age' => 22],
];

$arr = array_column($array,'name','id');

echo '<pre class="brush:php;toolbar:false">';
print_r($arr);

result:
Array
(
    [1] => tom
    [2] => jack
    [3] => mick
    [4] => lucy
    [5] => bill
)
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template