Use array unpacking (...) inside square bracket array literals for string-keyed and indexed array merging.
PHP 8.1 expanded array unpacking to support string keys inside array literals. This replaces array_merge() calls with clean spread syntax.
$defaults = ['theme' => 'dark', 'notifications' => true];
$userCustom = ['notifications' => false, 'language' => 'en'];
// Unpacks and merges arrays natively
$options = [...$defaults, ...$userCustom];
- Replaces verbose array_merge() calls with clean spread operator syntax
- Supports string keys as of PHP 8.1 (later values overwrite earlier keys)
- Cleaner syntax for array composition and middleware pipelines
Tags:
#PHP #Arrays #Syntax