Unpack Arrays In Arrays¶
The ellipsis operator can now be used in arrays, with an effect similar to array_merge(). In particular, the string keys are now supported.
PHP code¶
<?php
$array = [...['a' => 'foo'], ...['b' => 'bar']];
print_r($array);
?>
Before¶
PHP Fatal error: Cannot unpack array with string keys in /codes/unpack_arrays.php on line 3
Fatal error: Cannot unpack array with string keys in /codes/unpack_arrays.php on line 3
After¶
Array
(
[a] => foo
[b] => bar
)
PHP version change¶
This behavior changed in 8.1