• dotdev
  • Posts
  • PHP Unanimously Approves Short List Syntax for Array Destructuring

PHP Unanimously Approves Short List Syntax for Array Destructuring

The PHP RFC for square bracket syntax for array destructuring assignment has passed and will be included in PHP 7.1. The vote needed a 2/3 majority to be accepted, and it passed unanimously in favor.

Short array syntax was added to PHP 5.4, and it allowed you to define arrays in a square bracket style:

// Old array style 
$array = array(1, 2, 3); 
// 5.4 square bracket style. 
$array = [1, 2, 3];

The accepted proposal creates an alternative to using a “list” for destructuring an array. In all previous versions of PHP, this works like this:

list($a, $b, $c) = array(1, 2, 3);

Now you can extract using a square bracket just as you do for assignment:

[$a, $b, $c] = [1, 2, 3];
["a" => $a, "b" => $b] = ["a" => a, "b", => 2];

It also works in foreach loops:

foreach ($points as ["x" => $x, "y" => $y]) {
    var_dump($x, $y);
}

You can learn more from the PHP RFC and look for this in the PHP 7.1 release.

Reply

or to participate.