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 with a unanimous vote 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];

With the accepted proposal it creates an alternative to using “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 find out more from the [PHP RFC](https://wiki.php.net/rfc/short_list_syntax_ and look for this in the PHP 7.1 release.

Filed Under: NewsTagged with: