array_column($rows, $column) returns the value of $column from every row in a list of rows. It turns a table of records into a flat array of one field, which is what you need before implode, in_array, array_sum or array_search.
The rows can come from anywhere: a decoded JSON response, a CSV file, or a database query that returned an array of associative arrays.
Use the index key to build key value pairs
The third argument names a field whose value becomes the key of each result. array_column($users, 'name', 'id') is the usual way to build an id to name lookup, for example for a <select> list.
If two rows share the same index value, the later row overwrites the earlier one.
Index whole rows by id with null
Pass null as the column to keep each full row, re-keyed by the index field. Looking a row up by id is then a direct key access instead of a loop.
Build the index once and every lookup after that is instant, which matters when you look up many ids in a loop.
Find a row with array_column and array_search
To find which row has a given value, search the column and use the position you get back. Check the result with !== false, because a match in the first row returns 0.
This relies on $users having the keys 0, 1, 2, because array_column returns a list. If the array was filtered and has gaps, call array_values($users) first. The array_search page covers the false versus 0 pitfall in detail.
Sum, count or average a column
array_column pairs well with the math helpers: extract the field, then add it up.
For a value computed from several fields, like quantity times price, array_map is the tool, as in the last line.
array_column with objects
With a list of objects, array_column reads public properties. Private and protected properties are not visible to it.
The second call returns an empty array: password is private, so every object is skipped.
Rows with a missing field are skipped
array_column does not fill in a placeholder for a row that lacks the field. The result can be shorter than the input, so its positions no longer match the rows.
The first result has two phones and no hint that Bob had none. With the name as the index key, you can see whose number is whose, which is the safer form whenever some rows may lack the field.
Frequently Asked Questions
What does array_column do in PHP?
It takes a list of rows (arrays or objects) and returns the value of one field from each row: array_column($users, 'email') gives a flat array of emails. Rows that do not have the field are skipped.
How do I make a key value array from rows?
Pass the key field as the third argument: array_column($users, 'name', 'id') returns [id => name, ...]. Pass null as the second argument, array_column($users, null, 'id'), to keep whole rows indexed by id.
What does array_column do when a row is missing the field?
It skips that row, so array_column($people, 'phone') can return fewer values than there are rows and the positions no longer match. Add an index key, array_column($people, 'phone', 'name'), to see which row each value came from.
Does array_column work with objects?
Yes, with an array of objects it reads public properties, and also properties made available through __get when the class defines __isset. Private and protected properties are skipped.