Columns at the Old Royal Naval College, London.

The story of array_column() goes back to php|tek 2008, when Christian Flickinger suggested it to me. The functionality is something that nearly every PHP developer has had to implement in userland code at some point in their careers, and other programming languages, such as Python—through list comprehensions—and Ruby—through—provide easier facilities to perform this operation (there is also .pluck in Rails). So, I thought, “Why not PHP?”

It also gave me an opportunity to experience the PHP core contribution process, which was my primary motivation.

In addition to the function in core PHP, I wrote a polyfill userland library for versions of PHP earlier than 5.5, the intent of which is to fully mimic the core function, complete with the exact same triggered errors and warnings.

I’ve written a couple posts about array_column() and the userland library:

What It Does

My goal for array_column() was simplicity. It follows this function signature:

array array_column(array $input, mixed $columnKey[, mixed $indexKey])

Given a multi-dimensional array of data, array_column() returns the values from a single column of the input array, identified by the $columnKey. Optionally, you may provide an $indexKey to index the values in the returned array by the values from the $indexKey column in the input array.

For example, using the following array of data, we tell array_column() to return an array of just the last names, indexed by their record IDs.

$records = array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe'
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith'
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones'
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe'
$lastNames = array_column($records, 'last_name', 'id');

If we call print_r() on $lastNames, you’ll see a resulting array that looks a bit like this:

[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe