Backend Development 2 min read

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中文网 Courses
php中文网 Courses
php中文网 Courses
Array Unpacking with String Keys in PHP 8.1

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.

Backend DevelopmentphpPHP 8.1Array UnpackingString Keys
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.