Cannot use empty array entries in keyed array assignment

Description

The list() (also known as [] on the left side of the = sign), can skip some values, by using consecutive commas. This feature is only available with positional list(), which assign the n-th argument to the n-th element of the array.

Since PHP 7.1, list() accepts named element: this feature match the keys with their equivalent in the array, instead of matching the positions. With that feature, skipping an element in the array is useless: it is also not accepted by the PHP engine.

Example

<?php

$array = ['a' => 1, 3, 'b' => 2];
['a' => $a, , 'b' => $b] = $array;

// valid usage
[$a, , $b] = array_values($array);

?>

Solutions

  • Remove the extra comma.

  • Remove the keys and use a positional list().