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
Array.map—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:
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.
If we call
$lastNames, you’ll see a resulting array that looks a bit like this: