This article is a translation, original address: https://stitcher.io/blog/new-in-php-81#array-unpacking-with-string-keys-rfc
Array unpacking with string keys
Array unpacking is already allowed in PHP 7.4, but it only works with number key. The reason string keys were not supported before was because there was no consensus on how to merge array duplicates.
RFC cleanly solves this problem by following the following semantics array_merge
:
$array1 = ["a" => 1]; $array2 = ["b" => 2]; $array = ["a" => 0, ...$array1, ...$array2]; var_dump($array); // ["a" => 1, "b" => 2]
See RFC for details: https://wiki.php.net/rfc /array_unpacking_string_keys
The above is the detailed content of A big explanation of the new features of PHP8.1: Unpacking arrays using string keys. For more information, please follow other related articles on the PHP Chinese website!