Menu

PHP list() and Array Destructuring: [$a, $b] = $arr

[$a, $b] = $array assigns the first two elements to $a and $b in one line, and ['name' => $name] = $user does the same by key. list() is the older spelling. Covers skipping elements, swapping variables, multiple return values, destructuring in foreach and the ... spread operator.

This page includes runnable editors - edit, run, and see output instantly.

[$a, $b] = $array; takes the elements of an array apart into separate variables in one line: $a gets $array[0], $b gets $array[1]. list($a, $b) = $array; is the older spelling of the same thing, and ['name' => $name] = $user; does it by key.

Pairing destructuring with explode() is the classic use: split a string, name the pieces, move on.

Skip elements

Leave a position empty to skip it. You can also stop early: unused elements at the end are ignored.

Destructure an associative array by key

With keys, the order in the array does not matter and you take only the fields you need. The key is written before =>, the variable after it.

Patterns nest the same way for lists: [[$x1, $y1], [$x2, $y2]] = $line; reads two points at once. Since PHP 7.3 a variable in a pattern can also be a reference, [, &$second] = $counts;, so changing $second changes the array.

One assignment uses either positions or keys, never both. [$a, 'k' => $b] = $arr; stops with Cannot mix keyed and unkeyed array entries in assignments.

Swap two variables

Build an array of the two values in reverse order and destructure it back. No temporary variable needed.

Return multiple values from a function

A PHP function returns one value, but that value can be an array. Destructuring at the call site makes it read like several return values. For more than two or three values, return keys so the caller cannot mix up the order.

Destructuring in foreach

The same syntax works in the foreach line, so each row arrives already split into named variables. This is what people mean by "foreach with list()". More loop patterns are on the foreach page.

The spread operator (...)

The three dots unpack an array in place. In an array literal, [...$a, ...$b] merges arrays. In a function call, f(...$args) passes the elements as separate arguments. In a function definition, ...$args collects extra arguments into an array.

Before PHP 8.1, spreading an array with string keys was an error; now string keys are kept and numeric keys are renumbered, the same rules as array_merge(). Variadic parameters like ...$amounts are covered with the other parameter kinds on the functions page.

Common mistake: fewer elements than variables

If the array is shorter than the pattern, the missing variables become null and PHP prints a warning for each one. This happens with input you do not control, such as a line with missing fields. For [$name, $city, $country] = explode(',', 'Grace'); PHP prints:

Warning: Undefined array key 1 in /var/www/html/import.php on line 5

Warning: Undefined array key 2 in /var/www/html/import.php on line 5

Decide what a missing piece should be and pad the array before destructuring:

For keyed patterns, merge defaults in first: ['a' => $a, 'b' => $b] = $input + ['a' => null, 'b' => null];. The + operator only adds keys that $input does not already have.

Frequently Asked Questions

What does list() do in PHP?

list($a, $b) = $array; assigns $array[0] to $a and $array[1] to $b. Since PHP 7.1 the short form [$a, $b] = $array; does exactly the same, and both forms accept keys: ['id' => $id] = $row;.

How do I swap two variables in PHP?

Destructure a new array made from them in the opposite order: [$a, $b] = [$b, $a];. No temporary variable is needed.

Can I use list() with an associative array in PHP?

Yes, since PHP 7.1. Name the keys: ['name' => $name, 'email' => $email] = $user; or list('name' => $name) = $user;. Without keys, [$a, $b] = $user; looks for the keys 0 and 1 and gets null with a warning.

What are the three dots (...) in PHP?

The spread operator. In an array literal, [...$a, ...$b] copies the elements of both arrays into a new one. In a call, max(...$numbers) passes the elements as separate arguments. In a function definition, function sum(...$numbers) collects any number of arguments into an array.

What happens if the array has fewer elements than the variables in list()?

The extra variables get null, and PHP prints a warning such as Undefined array key 2. Pad the array first, for example [$a, $b, $c] = array_pad($parts, 3, null);.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED