Array Unpacking with String Keys in PHP 8.1
PHP 8.1 introduces support for array unpacking with string keys, extending the previous numeric‑key only capability, by defining merge semantics that prioritize later values, and the article demonstrates usage with code examples and links to the RFC for further details.
PHP 8.1 adds array unpacking with string keys, a feature that was unavailable in earlier versions because the language lacked a clear rule for merging duplicate keys.
The RFC resolves this by adopting array_merge semantics, where later values overwrite earlier ones when keys collide.
Example:
<code>$array1 = ["a" => 1];
$array2 = ["b" => 2];
$array = ["a" => 0, ...$array1, ...$array2];
var_dump($array); // ["a" => 1, "b" => 2]
</code>The resulting array shows that the string‑key unpacking works as expected, merging the arrays and keeping the last occurrence of each key.
For the full specification see the RFC at https://wiki.php.net/rfc/array_unpacking_string_keys and related articles on other PHP 8.1 features.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.