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

Array Destructuring ([] = …)

php PHP 7.1+ Intermediate

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 192
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 4 pings T 19 pings F 7 pings S 4 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 3 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
ChatGPT 142 Perplexity 14 Amazonbot 13 Google 8 Unknown AI 4 SEMrush 2 DuckDuckGo 1 Ahrefs 1
crawler 183 crawler_json 1 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