Home > Article > Backend Development > Introduction to php array_column() function (example)
Function introduction:
array_column() Returns an array whose value is the value of a single column in the input array.
(Recommended tutorial: php graphic tutorial)
Syntax:
array_column(array,column_key,index_key);
Parameters:
array Required. Specifies the multidimensional array (record set) to use.
column_key Required. The column whose value needs to be returned.
index_key Optional. The column that is the index/key of the returned array.
(Video tutorial recommendation: Introduction to Programming)
Example:
Take out the last_name column from the record set and use the corresponding "id" column as key value:
<?php // 可能从数据库中返回数组 $a = array( array( 'id' => 5698, 'first_name' => 'Peter', 'last_name' => 'Griffin', ), array( 'id' => 4767, 'first_name' => 'Ben', 'last_name' => 'Smith', ), array( 'id' => 3809, 'first_name' => 'Joe', 'last_name' => 'Doe', ) ); $last_names = array_column($a, 'last_name', 'id'); print_r($last_names); ?>
Output result:
Array ( [5698] => Griffin [4767] => Smith [3809] => Doe )
The above is the detailed content of Introduction to php array_column() function (example). For more information, please follow other related articles on the PHP Chinese website!