array_column()
Anonymous contributor
Anonymous contributor1 total contribution
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 benull
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.
All contributors
- Anonymous contributorAnonymous contributor1 total contribution
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.