list() / Short Array Destructuring
Also Known As
list()
PHP list assignment
array destructuring list
TL;DR
Assigns array elements to variables in a single expression — list($a, $b) or the shorthand [$a, $b] = $array.
Explanation
PHP's list() construct (and its [] shorthand since PHP 7.1) destructures arrays into named variables. PHP 7.1 enabled key-based destructuring (['name' => $name, 'age' => $age] = $row) for associative arrays, greatly improving database row handling. Nested destructuring is supported. list() is especially useful with foreach for unpacking tuple-like arrays: foreach ($rows as [$id, $name]) and for swapping variables: [$a, $b] = [$b, $a]. Accessing a non-existent key emits a notice — validate structure first.
Common Misconception
✗ list() and [] destructuring are fully interchangeable. They behave identically in PHP 7.1+ for most cases, but list() preserves historical left-to-right assignment order in some edge cases. The [] syntax is preferred in modern PHP for brevity and consistency.
Why It Matters
list() and its short form [] allow assignment of multiple variables from an array in one expression — commonly used with preg_match, explode, and database row destructuring.
Common Mistakes
- Using list() with associative arrays without specifying keys — positional destructuring of associative arrays is error-prone.
- Not checking that the source array has enough elements — undefined index notices on short arrays.
- Confusing list() (assignment) with array() (creation) — similar syntax, completely different purpose.
- Using list() in PHP < 7.1 for associative array destructuring — key-based list() was added in 7.1.
Code Examples
✗ Vulnerable
// Positional list() on associative array — order-dependent and fragile:
[$name, $email] = $user; // Relies on array key order — breaks if order changes
// Safe:
['name' => $name, 'email' => $email] = $user; // Explicit key mapping
✓ Fixed
// list() / [] — destructure arrays into variables
[$first, $second, $third] = [10, 20, 30];
// Skip elements
[, $second] = getCoordinates(); // ignore x, capture y
// Nested
[[$x1, $y1], [$x2, $y2]] = [[1,2],[3,4]];
// Associative (PHP 7.1+)
['name' => $name, 'age' => $age] = $user;
// In foreach — unpack each row
$points = [[1,2,'A'], [3,4,'B']];
foreach ($points as [$x, $y, $label]) {
echo "$label: ($x, $y)\n";
}
// Classic swap without temp variable
[$a, $b] = [$b, $a];
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
25
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 6
Perplexity 6
Google 4
Unknown AI 3
SEMrush 2
Majestic 1
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 20
crawler_json 3
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Use list() or the short [] syntax to unpack arrays into named variables — it makes intent clear and avoids repetitive $arr[0], $arr[1] index access
📦 Applies To
PHP 5.5+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
$a=$arr[0]; $b=$arr[1]; $c=$arr[2]; pattern that list() or [$a,$b,$c]=$arr would express better
Auto-detectable:
✓ Yes
rector
php-cs-fixer
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✓ Auto-fixable
Fix: Low
Context: Line