PHP array_column() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_column() takes a list of rows, such as records from a database, and returns the values of one column from every row. It saves you from writing a loop just to collect one field.

You use array_column() when you have an array of arrays (or objects) and you want a single list such as all names or all ids. You can also pick a second column to become the keys of the result, which makes quick lookup tables.

Syntax

Call array_column() with the values you need. Start with this line.

PHP
$names = array_column($users, 'name');
CallWhat it does
array_column($users, 'name')Returns a list of every user's name.
array_column($users, 'name', 'id')Returns the names, using each user's id as the key.
array_column($users, null, 'id')Returns the full rows, indexed by their id.

Each row can be an array or an object with public properties. Rows that do not contain the requested column are simply skipped. The original array is not changed.

Library declarations
PHP
array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array

Parameters

ParameterDescription
arrayRequired. A list of rows. Each row is an array or an object.
column_keyRequired. The column to return. Use a key name or an integer index. Use null to return the whole row.
index_keyOptional. The column whose values become the keys of the result. If a row has no such column, PHP uses the next automatic integer key for it.

Return Value

Return ValueDescription
arrayAn array of the column values, or an empty array when nothing matches.

PHP Examples

Example 1 — Getting One Column

Collect the name of every user from a list of records.

PHP
<?php
$users = [
    ['id' => 1, 'name' => 'Asha'],
    ['id' => 2, 'name' => 'Ravi'],
    ['id' => 3, 'name' => 'Meena'],
];

print_r(array_column($users, 'name'));

How It Works

  1. $users is a list of three rows and each row is an associative array.
  2. array_column($users, 'name') reads the 'name' value from every row.
  3. No index key was given, so the result uses the keys 0, 1 and 2.
  4. print_r() shows the three names in their original order.

Example 2 — Rows With a Missing Column

Rows that do not have the requested column are skipped. The third argument sets the keys.

PHP
<?php
$rows = [
    ['id' => 1, 'email' => '[email protected]'],
    ['id' => 2],
    ['id' => 3, 'email' => '[email protected]'],
];

print_r(array_column($rows, 'email', 'id'));

How It Works

  1. The second row has no 'email' column.
  2. array_column() skips that row without any warning.
  3. The 'id' values 1 and 3 become the keys of the result.
  4. The result has two items, even though the input had three rows.

Example 3 — Indexing Full Rows by id

Pass null as the column to keep the whole row, and use an index key to build a lookup table.

PHP
<?php
$users = [
    ['id' => 1, 'name' => 'Asha'],
    ['id' => 2, 'name' => 'Ravi'],
    ['id' => 3, 'name' => 'Meena'],
];

$byId = array_column($users, null, 'id');

echo $byId[2]['name'], PHP_EOL;
echo implode(',', array_keys($byId)), PHP_EOL;

How It Works

  1. The column key is null, so every full row is returned.
  2. The index key 'id' makes each row's id the key in the new array.
  3. $byId[2] now reads the row for id 2 directly, without any loop.
  4. array_keys() confirms that the keys are 1, 2 and 3.

Frequently Asked Questions

It returns the values of a single column from a list of rows. For example array_column($users, 'name') gives every name in the $users list.
Yes. If the rows are objects, array_column() reads their public properties. It also works with objects that provide the properties through the magic __get() and __isset() methods.
Pass the key column as the third argument, such as array_column($users, 'name', 'id'). The id values become the keys and the names become the values.
That row is skipped and no warning is shown. The result can therefore have fewer items than the input array.
Pass null as the second argument and the column name as the third, for example array_column($users, null, 'id'). Each row is returned whole and is stored under its id.

Next: array_combine()

array_combine() builds a new array by using the values of one array as keys and the values.

array_combine() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful