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

array_column() — Plucking Values from Arrays

php PHP 5.5+ Beginner

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 27
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 6 Google 3 ChatGPT 1 Ahrefs 1
crawler 18 crawler_json 1
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