Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP array_column() Function

PHP array_column() Function

PHP Array Reference ManualComplete PHP Array Reference Manual

Example

Get the last_name column from a record set:

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 values of a single column from 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 of values to return. Can be an integer index of a column in an indexed array, or a string key of a column in an associative array. This parameter can also be NULL, in which case the entire array is returned (very useful when resetting array keys with the index_key parameter).
index_key Optional. The column to use as the index/keys of the returned array.

Technical Details

Return Value: Returns an array whose values are from a single column of the input array.
PHP Version: 5.5+

More examples

Example 1

Get the last_name column from a record set, using the corresponding "id" column as keys:

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

)


PHP Array Reference ManualComplete PHP Array Reference Manual