array_column
Extracts the values of a single column from a multi-dimensional array or array of objects.
array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array- Added in PHP 5.5.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | A multi-dimensional array, or an array of objects, to pull the column from. |
$column_key | int|string|null | The key or property name of the column to return, or null to return the complete rows (useful together with $index_key). |
$index_keyoptional | int|string|null= null | The key or property whose values should be used as the keys of the returned array. |
Return value
arrayAn array of the values found under $column_key, optionally keyed by the values found under $index_key.
Errors
Throws a TypeError if a value used as $index_key is an object that cannot be used as an array key.
Changelog
- PHP 7.0.0Added the ability to work with an array of objects, reading public properties (and properties exposed through __isset()/__get()).
- PHP 8.0.0Objects in the column named by $index_key are no longer cast to string and now throw a TypeError.
array_column() takes a list of rows — associative arrays from a database fetch, or objects — and returns one field from every row as a flat array. Give it a second key and it also builds the result's keys from another field, turning a result set into a lookup table in a single call.
When to reach for it
It is the fastest way to answer "just give me the IDs" after a query: array_column($rows, 'id') feeds straight into an IN (...) clause or array_unique(). Pass both arguments to build an id => name map for a select box, or pass null as the column with an index key to re-key whole rows by their primary key so you can look one up in O(1) instead of looping.
What trips people up
Rows that are missing the requested column are skipped silently — no warning, no null placeholder — so the result can be shorter than the input. If you rely on positional alignment between two extracted columns, that silence will bite.
Duplicate values in the index column overwrite each other; the last row wins. When the index values are not unique, group with a loop instead.
Example
When to use it
- Turn a query result into a flat list of IDs for an IN (...) clause
- Build an id => label map for a <select> without writing a foreach
- Re-key rows by primary key so later lookups are O(1) instead of a linear search
Frequently asked questions
Does array_column work with an array of objects?
Yes, since PHP 7.0 it reads public properties by name, and properties made visible through __isset() and __get(). Private and protected properties are not accessible.
What happens if two rows have the same index_key value?
The later row overwrites the earlier one, so the result contains only the last match. Use a foreach and append to sub-arrays if you need to group rather than replace.
How do I keep rows that are missing the column?
You cannot with array_column — it drops them silently. Use array_map(fn($r) => $r['col'] ?? null, $rows) when you need a placeholder for every input row.
More examples
Re-keying whole rows
Passing null as the column returns the complete rows, keyed by SKU. The output shows a map you can index directly, which replaces a foreach that searches for the matching row.
<?php
$rows = [
['sku' => 'A-1', 'price' => 9.99, 'stock' => 4],
['sku' => 'B-2', 'price' => 19.5, 'stock' => 0],
];
$bySku = array_column($rows, null, 'sku');
print_r($bySku);
echo "B-2 stock: ", $bySku['B-2']['stock'], "\n";Working with objects
Since PHP 7.0 array_column reads public object properties too, so the output is [1 => 'Ada', 2 => 'Linus']. Private properties are not visible unless the class exposes them through __isset() and __get().
<?php
class User {
public function __construct(public int $id, public string $name) {}
}
$users = [new User(1, 'Ada'), new User(2, 'Linus')];
print_r(array_column($users, 'name', 'id'));Missing columns are dropped silently
The second row has no 'email' key, so it vanishes from the result without a warning: three rows produce two emails, and the returned keys 0 and 1 no longer correspond to the original row positions.
<?php
$rows = [
['id' => 1, 'email' => '[email protected]'],
['id' => 2],
['id' => 3, 'email' => '[email protected]'],
];
$emails = array_column($rows, 'email');
print_r($emails);
echo count($rows), " rows -> ", count($emails), " emails\n";
Discussion