{
    "slug": "array_destructuring",
    "term": "Array Destructuring ([] = …)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP's short list() syntax — [$a, $b] = $arr — assigns array elements to variables in a single expressive statement.",
    "long": "Array destructuring (PHP 7.1+) uses [] = syntax as shorthand for the older list() = form. Both positional and key-based forms are supported: [$first, $second] = $arr and ['name' => $name, 'age' => $age] = $row. Elements can be skipped: [, $second] = $pair. Particularly readable in foreach: foreach ($rows as ['id' => $id, 'email' => $email]). PHP 8.1 added support for destructuring in more contexts. The feature improves readability over multiple explicit index accesses and makes tuple-style returns from functions practical.",
    "aliases": [
        "array unpacking assignment",
        "list() assignment",
        "short list syntax"
    ],
    "tags": [
        "php",
        "php7",
        "syntax"
    ],
    "misconception": "Array destructuring only works with indexed arrays. PHP's list() and [] destructuring support key-based destructuring too — you can extract named keys in any order, skip elements, and nest destructuring for multi-dimensional arrays.",
    "why_it_matters": "Array destructuring makes extraction of multiple values from arrays explicit and readable, reducing the risk of off-by-one errors from manual index access and improving intent clarity.",
    "common_mistakes": [
        "Using list() or [] destructuring without checking that the array has the expected keys first — undefined index notices on missing keys.",
        "Destructuring numerically-indexed arrays by position when the array structure may change — use named keys instead.",
        "Not using the short [] syntax (PHP 7.1+) when list() is only being used for clarity — both work but [] is preferred.",
        "Ignoring values with a placeholder: [$first, , $third] is valid and clearer than skipping the index check."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "list_assignment",
        "arrow_functions",
        "named_arguments"
    ],
    "prerequisites": [
        "php_data_types",
        "arrow_functions",
        "generators"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.types.array.php#language.types.array.destructuring"
    ],
    "bad_code": "// Fragile index-based access instead of destructuring:\n$coords = getCoordinates();\n$lat = $coords[0]; // What's index 0?\n$lon = $coords[1];\n\n// Better:\n['lat' => $lat, 'lon' => $lon] = getCoordinates();",
    "good_code": "// Key-based in a loop\nforeach ($users as ['id' => $id, 'email' => $email]) {\n    sendNotification($id, $email);\n}",
    "quick_fix": "Use list() or the short array syntax [$a, $b] = $array for assignment; use ['key' => $val] for named key destructuring in PHP 7.1+",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/array_destructuring",
        "html_url": "https://codeclaritylab.com/glossary/array_destructuring",
        "json_url": "https://codeclaritylab.com/glossary/array_destructuring.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 Destructuring ([] = …)](https://codeclaritylab.com/glossary/array_destructuring) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/array_destructuring"
            }
        }
    }
}