array_column()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Aug 26, 2023
Contribute to Docs

The array_column() function returns the values from a single column in the input array.

Syntax

array_column(array, column_key, index_key)
  • array: A multi-dimensional array to use.
  • column_key: An integer or string key of the column of values to return. It may also be null to return complete arrays or objects.
  • index_key: The column to use as the index for the returned array.

Example

In this example, the first_name keys are returned using the array_column() method.

<?php
$a = array(
array(
'id' => 362,
'first_name' => 'Jane',
'last_name' => 'Doe',
),
array(
'id' => 921,
'first_name' => 'Lois',
'last_name' => 'Griffin',
),
array(
'id' => 475,
'first_name' => 'Lola',
'last_name' => 'Bunny',
)
);
$first_names = array_column($a, 'first_name');
print_r($first_names);
?>

The output looks like this:

Array
(
[0] => Jane
[1] => Lois
[2] => Lola
)

Additionally, the returned values can be indexed by the id column:

<?php
$first_names = array_column($a, 'first_name', 'id');
print_r($first_names);
?>

The output looks like this:

Array
(
[362] => Jane
[475] => Lola
[921] => Lois
)

Codebyte Example

The following codebyte example is runnable and utilizes the array_column() method.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn PHP on Codecademy