array_column() — Plucking Values from Arrays
debt(d7/e1/b1/t3)
Closest to 'only careful code review or runtime testing' (d7). No detection_hints.tools are specified. The common misuse — using array_search() in a loop instead of array_column() — is a performance anti-pattern that produces correct results silently. No standard linter or SAST tool flags this by default; it only surfaces via code review or profiling under load.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix is literally a one-line replacement: swap a foreach loop for array_column($rows, 'name', 'id'). No structural change required.
Closest to 'minimal commitment' (b1). array_column() is a single stdlib call at the point of use. It carries no architectural weight, no cross-cutting dependency, and no ongoing tax on future maintainers — it's a local transformation with no reach.
Closest to 'minor surprise (one edge case)' (t3). The misconception is that it only works with string keys; in fact it works with integer keys and, since PHP 7.0, with object arrays via public property access. This is a bounded gotcha — surprising when first encountered but documented and easy to internalise. The silent empty-array return on flat/non-array input is a secondary edge case, keeping the score at t3 rather than t5.
Also Known As
TL;DR
Explanation
array_column(array $array, int|string|null $column_key, int|string|null $index_key = null) has three modes. With just $column_key, it returns a flat array of all values for that key. With $column_key and $index_key, it returns an associative array indexed by the second key — building a lookup map in one call. With $column_key as null and $index_key set, it re-indexes the original rows by a key. Since PHP 7.0 it also accepts arrays of objects, accessing public properties. It is significantly faster than equivalent foreach loops for large arrays because it operates in C rather than userland PHP.
Common Misconception
Why It Matters
Common Mistakes
- Using array_search() in a loop instead of building an array_column() lookup map — O(n) per lookup vs O(1) after the O(n) one-time index build.
- Forgetting the third parameter when building lookup maps — array_column($rows, 'name') gives a list; array_column($rows, 'name', 'id') gives the ID-indexed map.
- Passing null as the second parameter — array_column($rows, null, 'id') re-indexes the full rows by ID, which is valid but different from extracting a column.
- Using array_column() on non-array data — it requires an array of arrays or objects; passing a flat array or a string returns an empty array silently.
Code Examples
<?php
// ❌ Manual loops for common array transformations
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com'],
];
// Building a name list
$names = [];
foreach ($users as $u) {
$names[] = $u['name'];
}
// Building an ID-indexed lookup map
$byId = [];
foreach ($users as $u) {
$byId[$u['id']] = $u;
}
<?php
// ✅ array_column() — one-liners for all three patterns
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com'],
];
// Extract a column
$names = array_column($users, 'name'); // ['Alice', 'Bob']
$emails = array_column($users, 'email'); // ['alice@example.com', ...]
// ID-indexed lookup map
$byId = array_column($users, null, 'id'); // [1 => [...], 2 => [...]]
$nameById = array_column($users, 'name', 'id'); // [1 => 'Alice', 2 => 'Bob']
// Lookup: O(1) instead of linear search
echo $nameById[2]; // 'Bob'
// Also works with objects (PHP 7.0+)
class User { public int $id; public string $name; }
$objects = [/* ... */];
$names = array_column($objects, 'name');