Understanding the PHP Function array_column()
The PHP function array_column() is a handy tool for extracting values from arrays, and transforming them into new arrays. It is often used when dealing with
The PHP function array_column() extracts the values of a single column from a multi-dimensional array (or an array of objects) and returns them as a new, flat array. It is one of the most useful array functions when you work with data that comes back from a database, a CSV file, or a JSON API as a list of records, because it lets you pull out just the field you care about in a single line — without writing a foreach loop.
This chapter covers the function's syntax, how the optional index_key parameter re-keys the result, how it behaves with arrays of objects and missing keys, common use cases, and the gotchas to watch for. For broader context, see PHP Arrays and Multidimensional Arrays.
What is the array_column() Function?
The array_column() function is a PHP built-in function that is used to extract values from an array, and return them as a new array. It takes two required parameters: the array that you want to extract values from, and the key (or column) that you want to extract. The function returns a new array that contains only the values from the specified column.
Here's the basic syntax of the array_column() function:
PHP array_column function syntax
array_column(array, column_key, [index_key]);array: This is the array that you want to extract values from.column_key: This is the key (or column) that you want to extract.index_key(optional): This is the key whose value is used as the index (key) of the returned array. Passnullforcolumn_key(since PHP 7.0) to return whole rows re-indexed byindex_key.
The function returns a brand-new array; it never modifies the original. It works on arrays whose elements are themselves arrays or objects with public properties.
How to Use the array_column() Function
Using the array_column() function is quite simple. All you need to do is pass in an array and the key that you want to extract. Here's an example that demonstrates how to use array_column():
PHP example of array_column function
<?php
$data = [
[
'id' => 1,
'name' => 'John',
'age' => 30,
],
[
'id' => 2,
'name' => 'Jane',
'age' => 25,
],
[
'id' => 3,
'name' => 'Jim',
'age' => 35,
],
];
$names = array_column($data, 'name');
print_r($names);
?>The output of this code would be:
Array
(
[0] => John
[1] => Jane
[2] => Jim
)In this example, the array_column() function has extracted the values from the name column and returned them as a new, numerically-indexed array.
Using the index_key Parameter
The index_key parameter allows you to specify a key to use as the index of the returned array. Here's an example that demonstrates how to use the index_key parameter:
Example of PHP function array_column with more arguments
<?php
$data = [
[
'id' => 1,
'name' => 'John',
'age' => 30,
],
[
'id' => 2,
'name' => 'Jane',
'age' => 25,
],
[
'id' => 3,
'name' => 'Jim',
'age' => 35,
],
];
$names = array_column($data, 'name', 'id');
print_r($names);
?>The output of this code would be:
Array
(
[1] => John
[2] => Jane
[3] => Jim
)In this example, array_column() extracted the values from the name column and used the id column as the key of each entry in the returned array. This is the fastest way to build a lookup table keyed by ID.
Returning Whole Rows with a null column_key
Since PHP 7.0 you can pass null as the column_key. The function then keeps each entire row but re-indexes the result by index_key. This is handy when you want a record-by-ID map rather than a single field:
<?php
$data = [
['id' => 1, 'name' => 'John', 'age' => 30],
['id' => 2, 'name' => 'Jane', 'age' => 25],
];
$byId = array_column($data, null, 'id');
print_r($byId);
?>Working with Arrays of Objects
array_column() also reads public properties of objects, so you can use it on a list of model instances exactly as you would with arrays:
<?php
class User {
public function __construct(public int $id, public string $name) {}
}
$users = [
new User(1, 'John'),
new User(2, 'Jane'),
];
print_r(array_column($users, 'name', 'id'));
?>Gotchas to Watch For
- Missing keys are skipped silently. If a row does not contain
column_key, that row is simply omitted from the result — no warning is raised. Likewise, a row missing theindex_keyfalls back to a sequential numeric key. - Duplicate
index_keyvalues overwrite each other. Because keys must be unique, if two rows share the sameindex_key, only the last one survives. - Only public object properties are read. Protected and private properties are ignored unless the class implements magic accessors.
Use Cases for array_column()
The array_column() function can be used in a variety of situations where you need to extract values from arrays and return them as a new array. Here are a few common use cases:
- Extracting values from multi-dimensional arrays to create a simple list
- Creating an associative array from a multi-dimensional array
- Transforming a multi-dimensional array into a single-dimensional array for use in a drop-down list or other form element
If you also need to transform each value while extracting it, reach for array_map(); to keep only the rows that match a condition, see array_filter().
Conclusion
The array_column() function is a concise tool for extracting a single column from multi-dimensional arrays and arrays of objects. With the optional index_key, you can re-key the result in one step, and with a null column_key you can build whole-record lookup maps. Reaching for it instead of a hand-written foreach loop makes data-shaping code shorter and easier to read.