Home  >  Article  >  Backend Development  >  A big explanation of the new features of PHP8.1: Unpacking arrays using string keys

A big explanation of the new features of PHP8.1: Unpacking arrays using string keys

藏色散人
藏色散人Original
2021-11-10 15:30:492614browse

This article is a translation, original address: https://stitcher.io/blog/new-in-php-81#array-unpacking-with-string-keys-rfc

A big explanation of the new features of PHP8.1: Unpacking arrays using string keys

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn