PHP array_column() Function

PHP array_column Function


The PHP array_column() function returns an array that contains specific column values of the given array.

Syntax of the array_column()

   array_column(array, column_key, index_key);

The PHP array column function takes three parameters –

  1. array – An array.
  2. column_key – The column name or key which you want to return.
  3. index_key – It is an optional parameter. It is used to set a column value to the index key of the returning value.

Example:

In the following example, we separate specific column values from an array (this array represents a possible recordset returned from a database).

Output : Array ( [0] => John [1] => Mark [2] => Barry [3] => Peter )

index_key Parameter Example :

Output : 
Array
(
    [5211] => web
    [5421] => online
    [6523] => alex
    [7541] => Peter
)