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

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

Jun 06, 2016 pm 07:39 PM
ar array multidimensional array Convert key value

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

XREAL founder Xu Chi: Apple lets everyone see the future of spatial computing, and we work hard to let everyone see the present of spatial computing. XREAL founder Xu Chi: Apple lets everyone see the future of spatial computing, and we work hard to let everyone see the present of spatial computing. Jun 02, 2024 pm 07:55 PM

XREAL founder Xu Chi: Apple lets everyone see the future of spatial computing, and we work hard to let everyone see the present of spatial computing.

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

How to remove duplicate elements from PHP array using foreach loop?

NTT QONOQ Devices unveils Mirza wireless XR glasses for smartphones NTT QONOQ Devices unveils Mirza wireless XR glasses for smartphones Sep 09, 2024 pm 06:30 PM

NTT QONOQ Devices unveils Mirza wireless XR glasses for smartphones

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

PHP array key value flipping: Comparative performance analysis of different methods

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

PHP array multi-dimensional sorting practice: from simple to complex scenarios

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

Christie: dual drive of technology + innovation brings unlimited possibilities Christie: dual drive of technology + innovation brings unlimited possibilities Apr 23, 2024 am 08:10 AM

Christie: dual drive of technology + innovation brings unlimited possibilities

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

Application of PHP array grouping function in data sorting

See all articles