Current Location: Home> Function Categories> array_column

array_column

Returns the value of a single column in the input array
Name:array_column
Category:Array
Programming Language:php
One-line Description:Returns the value of a single column in the input array.

Definition and usage

array_column() returns the value of a single column in the input array.

Example

Example 1

Remove the last_name column from the record set:

 <?php
// Array of possible records returned by the database
$a = array (
  array (
    'id' => 5698 ,
    'first_name' => 'Bill' ,
    'last_name' => 'Gates' ,
  ) ,
  array (
    'id' => 4767 ,
    'first_name' => 'Steve' ,
    'last_name' => 'Jobs' ,
  ) ,
  array (
    'id' => 3809 ,
    'first_name' => 'Mark' ,
    'last_name' => 'Zuckerberg' ,
  )
) ;

$last_names = array_column ( $a , 'last_name' ) ;
print_r ( $last_names ) ;
?>

Output:

 Array
(
  [0] => Gates
  [1] => Jobs
  [2] => Zuckerberg
)

Example 2

Take the last_name column from the record set and use the corresponding "id" column as the key value:

 <?php
// Array of possible records returned by the database
$a = array (
  array (
    'id' => 5698 ,
    'first_name' => 'Bill' ,
    'last_name' => 'Gates' ,
  ) ,
  array (
    'id' => 4767 ,
    'first_name' => 'Steve' ,
    'last_name' => 'Jobs' ,
  )
  array (
    'id' => 3809 ,
    'first_name' => 'Mark' ,
    'last_name' => 'Zuckerberg' ,
  )
) ;

$last_names = array_column ( $a , 'last_name' , 'id' ) ;
print_r ( $last_names ) ;
?>

Output:

 Array
(
  [5698] => Gates
  [4767] => Jobs
  [3809] => Zuckerberg
)

grammar

 array_column ( array , column_key , index_key ) ;
parameter describe
array Required. Specifies the multi-dimensional array (record set) to be used.
column_key

Required. Columns that need to return the value.

It can be an integer index of the columns that index the array, or a string key value of the columns that are associated with the array.

This parameter can also be NULL, and the entire array will be returned at this time (it is very useful when resetting the array key with the index_key parameter).

index_key Optional. Column used as the index/key of the array.
Similar Functions
Popular Articles