PHP Spread Syntax in Array Declaration

The spread operator in the arrays RFC has been implemented in PHP 7.4:

$ary = [3, 4, 5];
return [1, 2, ...$ary]; // same as [1, 2, 3, 4, 5]

Caveat: The unpacked array/Traversable can only have integer keys. For string keys array_merge() is still required.

Leave a Comment