{
    "slug": "php_array_column",
    "term": "array_column() — Plucking Values from Arrays",
    "category": "php",
    "difficulty": "beginner",
    "short": "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.",
    "long": "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.",
    "aliases": [
        "array_column",
        "PHP pluck",
        "PHP extract column",
        "array lookup map"
    ],
    "tags": [
        "php-stdlib",
        "arrays",
        "performance",
        "collections"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_array_functions_fp",
        "array_functions",
        "php_csv_handling",
        "n_plus_one"
    ],
    "prerequisites": [],
    "refs": [
        "https://www.php.net/array_column"
    ],
    "bad_code": "<?php\n// ❌ Manual loops for common array transformations\n$users = [\n    ['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],\n    ['id' => 2, 'name' => 'Bob',   'email' => 'bob@example.com'],\n];\n\n// Building a name list\n$names = [];\nforeach ($users as $u) {\n    $names[] = $u['name'];\n}\n\n// Building an ID-indexed lookup map\n$byId = [];\nforeach ($users as $u) {\n    $byId[$u['id']] = $u;\n}",
    "good_code": "<?php\n// ✅ array_column() — one-liners for all three patterns\n$users = [\n    ['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com'],\n    ['id' => 2, 'name' => 'Bob',   'email' => 'bob@example.com'],\n];\n\n// Extract a column\n$names = array_column($users, 'name');          // ['Alice', 'Bob']\n$emails = array_column($users, 'email');        // ['alice@example.com', ...]\n\n// ID-indexed lookup map\n$byId   = array_column($users, null, 'id');     // [1 => [...], 2 => [...]]\n$nameById = array_column($users, 'name', 'id'); // [1 => 'Alice', 2 => 'Bob']\n\n// Lookup: O(1) instead of linear search\necho $nameById[2]; // 'Bob'\n\n// Also works with objects (PHP 7.0+)\nclass User { public int $id; public string $name; }\n$objects = [/* ... */];\n$names = array_column($objects, 'name');",
    "quick_fix": "Replace 'foreach ($rows as $r) { $map[$r['id']] = $r['name']; }' with 'array_column($rows, 'name', 'id')'.",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_array_column",
        "html_url": "https://codeclaritylab.com/glossary/php_array_column",
        "json_url": "https://codeclaritylab.com/glossary/php_array_column.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[array_column() — Plucking Values from Arrays](https://codeclaritylab.com/glossary/php_array_column) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_array_column"
            }
        }
    }
}