Spread Operator in Array Expressions

The spread operator ..., used to unpack arguments, has already been introduced in PHP 5.6:

var_dump(...[1, 2, 3]);

It converts an array to a list of elements:

int(1)
int(2)
int(3)

The upside of argument unpacking is that instead of passing around an untyped array, you can add a type declaration and thus make sure all passed arguments have the same type.

Since PHP 7.4, the so-called splat operator can directly be used inside arrays:

$list = [3, 4, 5];

var_dump([1, 2, ...$list, 6]);

This will output:

array(6) {
  [0] => int(1)
  [1] => int(2)
  [2] => int(3)
  [3] => int(4)
  [4] => int(5)
  [5] => int(6)
}

There are two limitations that you need to be aware of. First, arrays cannot be assoiative, they must be index-based. Second, you cannot unpack an array by reference:

$list = [3, 4, 5];

var_dump(...&$list);

Trying to use a reference operator directly leads to a syntax error:

PHP Parse error:  syntax error, unexpected '&' in ...