← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

array_column() — Plucking Values from Arrays

PHP PHP 5.5+ Beginner
debt(d7/e1/b1/t3)
d7 Detectability Operational debt — how invisible misuse is to your safety net

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.

e1 Effort Remediation debt — work required to fix once spotted

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.

b1 Burden Structural debt — long-term weight of choosing wrong

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.

t3 Trap Cognitive debt — how counter-intuitive correct behaviour is

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.

About DEBT scoring →

Also Known As

array_column PHP pluck PHP extract column array lookup map

TL;DR

array_column() extracts a single column from a multi-dimensional array or array of objects — the idiomatic PHP replacement for foreach loops that build lookup arrays, and for creating ID-indexed maps.

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

array_column() only works with string keys. It works with integer keys too, and since PHP 7.0 it works with object arrays accessing public properties — array_column($objects, 'name') works on arrays of objects.

Why It Matters

Building ID-to-name lookup maps from database result sets is one of the most common PHP patterns. array_column($users, 'name', 'id') does it in one line instead of a four-line foreach. Using it consistently also signals intent clearly — a reader immediately knows a transformation is happening, not just iteration.

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

✗ Vulnerable
<?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;
}
✓ Fixed
<?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');

Added 23 Mar 2026
Views 54
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 2 pings T 0 pings F 0 pings S 2 pings S 1 ping M 3 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 9 Google 6 Perplexity 6 Scrapy 5 ChatGPT 4 Ahrefs 3 SEMrush 3 Claude 2 Bing 1 Meta AI 1 Majestic 1 PetalBot 1
crawler 38 crawler_json 4
DEV INTEL Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Replace 'foreach ($rows as $r) { $map[$r['id']] = $r['name']; }' with 'array_column($rows, 'name', 'id')'.
📦 Applies To
PHP 5.5+ web cli


✓ schema.org compliant