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

Array Unpacking with String Keys (PHP 8.1)

php PHP 8.1+ Intermediate
debt(d3/e1/b1/t5)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). Tools like Rector and PHPStan (listed in detection_hints.tools) can detect usage of array_merge() where spread operator would be cleaner, and can flag string-key spread usage on incompatible PHP versions. These are commonly configured tools in PHP projects, slightly more specialized than a basic linter but readily available, so d3 fits.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix confirms this is a direct replacement: array_merge($defaults, $overrides) becomes [...$defaults, ...$overrides]. This is a single-expression swap with no structural change required.

b1 Burden Structural debt — long-term weight of choosing wrong

Closest to 'minimal commitment' (b1). This is a syntax-level feature for array construction. It's a local expression-level choice that doesn't impose any structural weight on the codebase. Each usage is independent and trivially reversible back to array_merge().

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field identifies the key trap: developers assume the spread operator has always worked with string-keyed arrays, but it was only added in PHP 8.1 (PHP 7.4 only supported numeric keys). Additionally, common_mistakes notes that duplicate string keys in spread cause an error in PHP 8.1, unlike array_merge() where later values win — this contradicts the expected merge semantics that most PHP developers are familiar with. These are documented gotchas that most devs eventually learn.

About DEBT scoring →

Also Known As

string key array unpacking PHP 8.1 array spread spread operator strings

TL;DR

PHP 8.1 allows the spread operator to unpack arrays with string keys — previously only integer-keyed arrays could be spread.

Explanation

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.

Common 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.

Code Examples

✗ Vulnerable
// Duplicate string key in spread — error in PHP 8.1:
$defaults = ['color' => 'blue', 'size' => 'M'];
$overrides = ['color' => 'red'];
$merged = [...$defaults, ...$overrides]; // Error: duplicate key 'color'
// Fix: use array_merge($defaults, $overrides) for override semantics
✓ Fixed
$defaults = ['timeout' => 30, 'retries' => 3];
$options  = ['timeout' => 60];
$config   = [...$defaults, ...$options]; // ['timeout'=>60,'retries'=>3]

Added 15 Mar 2026
Edited 22 Mar 2026
Views 24
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings 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 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 1 ping S
Amazonbot 10 Perplexity 5 Google 2 Unknown AI 2 Ahrefs 2
crawler 19 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
PHP 8.1 allows array unpacking with string keys: [...$defaults, ...$overrides] — it replaces array_merge() for combining arrays without reindexing numeric keys
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
array_merge($a, $b) where spread operator would be cleaner; PHP 8.1+ not using string key unpacking
Auto-detectable: ✓ Yes rector phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Line

✓ schema.org compliant