Array Unpacking with String Keys
PHP 7.4 introduced the spread operator for arrays, but it only worked with integer keys. PHP 8.1 completed the feature by supporting string keys — making array merging clean and expressive.
$defaults = ['timeout' => 30, 'retries' => 3, 'verify' => true];
$custom = ['timeout' => 60, 'debug' => true];
// Clean, readable merge with override semantics
$config = [...$defaults, ...$custom];
// ['timeout' => 60, 'retries' => 3, 'verify' => true, 'debug' => true]
// Works beautifully in function calls too
function createClient(string $host, array $options = []) {
$opts = [...self::DEFAULT_OPTIONS, ...$options];
// ...
}
The spread operator for arrays follows the same ... syntax used for function arguments, keeping the language consistent. Later values override earlier ones, just like phparray_merge, but with cleaner syntax.
Significance: Expressiveness
Small syntactic improvements compound. The spread operator for arrays saves a function call, reads more naturally, and brings PHP's array handling in line with modern JavaScript and Python. It's one less reason to reach for
array_merge().