{
    "slug": "php_spread_in_arrays",
    "term": "Spread Operator in Arrays [...$a, ...$b]",
    "category": "php",
    "difficulty": "beginner",
    "short": "PHP 8.1+ allows the spread operator ... inside array literals to merge arrays — cleaner than array_merge() for combining arrays inline.",
    "long": "PHP 7.4 added spread in function calls. PHP 8.1 added spread in array expressions: [...$a, ...$b] creates a new merged array. Key behavior: integer keys are re-indexed (like array_merge), string keys from later arrays overwrite earlier ones (like array_merge). PHP 8.1 also allows string-keyed spreads. Spread preserves no reference — it creates copies. For associative merging with last-write-wins semantics, [...$defaults, ...$overrides] is idiomatic.",
    "aliases": [],
    "tags": [
        "php",
        "arrays",
        "php81",
        "spread"
    ],
    "misconception": "Spread in arrays preserves original integer keys — like array_merge(), integer keys are re-indexed from 0.",
    "why_it_matters": "Array spreading with ... removes the need for array_merge() in most cases, which has subtle behavioural differences with numeric keys — array_merge() reindexes, spread preserves. In config merging, test fixture building, and variadic function calls, spread syntax produces cleaner, more predictable code. It also enables named arguments spread from associative arrays in PHP 8.1+, which is useful for forwarding options through a call chain.",
    "common_mistakes": [
        "Expecting integer keys to be preserved — they're re-indexed.",
        "Using spread with string keys before PHP 8.1 — fatal error in PHP 7.4 spread.",
        "Not knowing that [...$a, ...$b] and array_merge($a, $b) behave identically for simple cases."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "spread_operator",
        "named_arguments_spread",
        "array_functions",
        "array_unpacking_strings"
    ],
    "prerequisites": [
        "array_functions"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.types.array.php"
    ],
    "bad_code": "$merged = array_merge($defaults, $userConfig, ['debug' => false]);\n// Works but verbose",
    "good_code": "// PHP 8.1+ idiomatic:\n$config = [...$defaults, ...$userConfig, 'debug' => false];\n\n// Cloning with overrides:\n$updated = [...$original, 'status' => 'active', 'updated_at' => now()];\n\n// Conditional spread:\n$params = [\n    'limit' => 20,\n    ...($search ? ['q' => $search] : []),\n];",
    "quick_fix": "Replace array_merge($a, $b) with [...$a, ...$b] for clearer array combination. Use [...$defaults, ...$overrides] for config merging patterns.",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_spread_in_arrays",
        "html_url": "https://codeclaritylab.com/glossary/php_spread_in_arrays",
        "json_url": "https://codeclaritylab.com/glossary/php_spread_in_arrays.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": "[Spread Operator in Arrays [...$a, ...$b]](https://codeclaritylab.com/glossary/php_spread_in_arrays) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_spread_in_arrays"
            }
        }
    }
}