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

Array Destructuring ([] = …)

PHP PHP 7.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). The detection_hints list rector and php-cs-fixer, both of which can automatically detect manual indexed access patterns ($a = $arr[0]; $b = $arr[1]) that could be replaced with destructuring syntax. These are standard PHP toolchain tools, not specialist SAST tools, so d3 fits well.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is literally replacing manual index assignments with [$a, $b] = $array or ['key' => $val] = $array — a direct syntactic swap that touches one line or a small cluster of lines. Rector can even automate this transformation.

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

Closest to 'minimal commitment' (b1). Array destructuring is a local syntactic choice — it's pure syntax sugar for variable assignment. It doesn't impose any architectural weight or structural commitment on the codebase. Each usage is self-contained and trivially reversible. Tags confirm it's a syntax-level concern.

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

Closest to 'notable trap — a documented gotcha most devs eventually learn' (t5). The misconception field states that developers believe destructuring only works with indexed arrays, when in fact PHP 7.1+ supports key-based destructuring in any order. This is a genuine surprise for developers coming from other languages (e.g. JavaScript) where destructuring objects vs arrays works differently. Additionally, common_mistakes include not checking for expected keys (leading to undefined index notices) and not knowing about skipping elements with placeholders. These are non-obvious behaviors that competent developers will guess wrong about initially.

About DEBT scoring →

Also Known As

array unpacking assignment list() assignment short list syntax

TL;DR

PHP's short list() syntax — [$a, $b] = $arr — assigns array elements to variables in a single expressive statement.

Explanation

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.

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

Code Examples

✗ Vulnerable
// Fragile index-based access instead of destructuring:
$coords = getCoordinates();
$lat = $coords[0]; // What's index 0?
$lon = $coords[1];

// Better:
['lat' => $lat, 'lon' => $lon] = getCoordinates();
✓ Fixed
// Key-based in a loop
foreach ($users as ['id' => $id, 'email' => $email]) {
    sendNotification($id, $email);
}

Tags


Added 15 Mar 2026
Edited 22 Mar 2026
Views 257
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 3 pings T 0 pings F 2 pings S 1 ping S 1 ping M 1 ping T 0 pings W 3 pings T 1 ping F 1 ping S 3 pings S 1 ping M 2 pings T 2 pings W 2 pings T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 1 ping W 0 pings T 1 ping F 2 pings S 3 pings S 0 pings M 1 ping T 0 pings W
No pings yet today
Perplexity 1
ChatGPT 142 Perplexity 35 Amazonbot 16 Scrapy 10 Google 9 Unknown AI 4 SEMrush 4 Ahrefs 4 Claude 2 DuckDuckGo 1 Meta AI 1 Bing 1 Qwen 1 Common Crawl 1 PetalBot 1 Sogou 1
crawler 229 crawler_json 3 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ 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+
📦 Applies To
PHP 7.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
$a = $arr[0]; $b = $arr[1]; pattern that could be [$a,$b] = $arr; indexed access when destructuring is cleaner
Auto-detectable: ✓ Yes rector php-cs-fixer
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Function


✓ schema.org compliant