array_column() function in php
Get the last_name column from the recordset:
<?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'); print_r($last_names); ?>
Output:
Array ( [0] => Griffin [1] => Smith [2] => Doe )
Definition and usage
array_column() Returns the value of a single column in the input array.
Syntax
array_column(array,column_key,index_key);
Parameters
Description
array Required. Specifies the multidimensional array (record set) to use.
column_key Required. The column whose value needs to be returned. Can be an integer index of a column of an index array, or a string key value of a column of an associative array. This parameter can also be NULL, in which case the entire array will be returned (very useful when used with the index_key parameter to reset the array key).
index_key Optional. The column that is the index/key of the returned array.
Technical details
Return value:
Returns an array whose value is the value of a single column in the input array.
Get the last_name column from the record set and use the corresponding "id" column as the 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:
Array ( [5698] => Griffin [4767] => Smith [3809] => Doe )
Complete PHP Array Reference Manual
The above is the detailed content of array_column() function in php. For more information, please follow other related articles on the PHP Chinese website!