PHP Function Reference

PHP array_column() Function



The PHP array_column() function returns the values from a single column of the array, identified by the column_key. Optionally, an index_key can also be used to index the values in the returned array by the values from the index_key column of the input array.

Syntax

array_column(array, column_key, index_key)

Parameters

array Required. Specify a multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic methods.
column_key Required. Specify an integer key or a string key name of the column of values to return. It may also be null to return complete arrays or objects (this is useful together with index_key to reindex the array).
index_key Optional. Specify an integer key or a string key name of the column to use as the index/keys for the returned array.

Return Value

Returns an array of values representing a single column from the input array.

Example: get the column of name from a recordset

The example below shows the usage of array_column() function.

<?php
//creating an array representing a possible
//record set returned from a database
$info = array(
  array(
    'id' => '101',
    'name' => 'John',
    'city' => 'London',
  ),
  array(
    'id' => '102',
    'name' => 'Marry',
    'city' => 'New York',
  ),
  array(
    'id' => '103',
    'name' => 'Sam',
    'city' => 'Paris',
  ),
  array(
    'id' => '104',
    'name' => 'Ramesh',
    'city' => 'Mumbai',
  )
);
 
//getting the column of name
$emp_name = array_column($info, 'name');
print_r($emp_name);

//getting the column of name 
//indexed by the "id" column
$emp_name_id = array_column($info, 'name', 'id');
print_r($emp_name_id);
?>

The output of the above code will be:

Array
(
    [0] => John
    [1] => Marry
    [2] => Sam
    [3] => Ramesh
)
Array
(
    [101] => John
    [102] => Marry
    [103] => Sam
    [104] => Ramesh
)

Example: get the column of usernames from the public property of an object

The example below shows how to get a column of "usernames" from the public property of the given object.

<?php
class User {
  public $username;

  public function __construct(string $userid) {
    $this->username = $userid;
  }
}

//creating an object of User class
$users = [
  new User('John123'),
  new User('Marry_K'),
  new User('Kim_Lo'),
];

//getting the column of username from 
//public property of the object
print_r(array_column($users, 'username'));
?>

The output of the above code will be:

Array
(
    [0] => John123
    [1] => Marry_K
    [2] => Kim_Lo
)

Example: get the column of usernames from the private property of an object

Consider the example below which demonstrates how to get a column of "usernames" from the private property of the given object. Please note the implementation of __get() and __isset() magic methods which is required to pull protected or private properties of an object.

<?php
class User {
  private $username;

  public function __construct(string $userid) {
    $this->username = $userid;
  }

  public function __get($prop) {
    return $this->$prop;
  }

  public function __isset($prop) : bool {
    return isset($this->$prop);
  }  
}

//creating an object of User class
$users = [
  new User('John123'),
  new User('Marry_K'),
  new User('Kim_Lo'),
];

//getting the column of username from 
//private property of the object
print_r(array_column($users, 'username'));
?>

The output of the above code will be:

Array
(
    [0] => John123
    [1] => Marry_K
    [2] => Kim_Lo
)

❮ PHP Array Reference