{
    "slug": "array_unpacking_strings",
    "term": "Array Unpacking with String Keys (PHP 8.1)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP 8.1 allows the spread operator to unpack arrays with string keys — previously only integer-keyed arrays could be spread.",
    "long": "Prior to PHP 8.1, the spread operator (...) only worked with integer-keyed arrays — spreading an associative array caused a fatal error. PHP 8.1 lifted this restriction, enabling array merging as an alternative to array_merge(): $merged = [...$defaults, ...$overrides]. This is cleaner and slightly faster than array_merge() for simple cases. String key conflicts are resolved by last-write-wins, identical to array_merge() semantics. Combined with named argument spreading, this enables concise configuration merging and option-bag patterns common in framework APIs.",
    "aliases": [
        "string key array unpacking",
        "PHP 8.1 array spread",
        "spread operator strings"
    ],
    "tags": [
        "php8",
        "php",
        "arrays",
        "syntax"
    ],
    "misconception": "The spread operator (...) has always worked with string-keyed arrays in PHP. String key support was added in PHP 8.1 — earlier versions throw a fatal error when spreading string-keyed arrays. Numeric-keyed spreading has been supported since PHP 7.4.",
    "why_it_matters": "PHP 8.1 enabled array unpacking with string keys, completing the spread operator for associative arrays — useful for building option arrays and merging configurations with override semantics.",
    "common_mistakes": [
        "Using array_merge() when the spread operator provides the same result with clearer intent for config overrides.",
        "Expecting later duplicate keys to win in spread operations — unlike array_merge, duplicate string keys in spread cause an error in PHP 8.1.",
        "Using numeric-keyed arrays with the spread operator expecting reindexing — behaviour differs from associative arrays.",
        "Not realising PHP 7 only supported numeric array unpacking — code using string key spread fails on PHP 7."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "spread_operator",
        "named_arguments_spread",
        "array_functions"
    ],
    "prerequisites": [
        "array_destructuring",
        "php_data_types",
        "arrow_functions"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.types.array.php"
    ],
    "bad_code": "// Duplicate string key in spread — error in PHP 8.1:\n$defaults = ['color' => 'blue', 'size' => 'M'];\n$overrides = ['color' => 'red'];\n$merged = [...$defaults, ...$overrides]; // Error: duplicate key 'color'\n// Fix: use array_merge($defaults, $overrides) for override semantics",
    "good_code": "$defaults = ['timeout' => 30, 'retries' => 3];\n$options  = ['timeout' => 60];\n$config   = [...$defaults, ...$options]; // ['timeout'=>60,'retries'=>3]",
    "quick_fix": "PHP 8.1 allows array unpacking with string keys: [...$defaults, ...$overrides] — it replaces array_merge() for combining arrays without reindexing numeric keys",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/array_unpacking_strings",
        "html_url": "https://codeclaritylab.com/glossary/array_unpacking_strings",
        "json_url": "https://codeclaritylab.com/glossary/array_unpacking_strings.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 Unpacking with String Keys (PHP 8.1)](https://codeclaritylab.com/glossary/array_unpacking_strings) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/array_unpacking_strings"
            }
        }
    }
}