Skip to content

Commit 2ff6e6b

Browse files
mumumuGirgias
andauthored
[PHP 8.1] Document Array Unpacking with [int|string] Keys. (#1171)
Co-authored-by: George Peter Banyard <[email protected]>
1 parent cd6ad4d commit 2ff6e6b

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

appendices/migration81/new-features.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,17 @@
3131
<title>Array Unpacking with String Keys</title>
3232

3333
<para>
34-
Added support for array unpacking with strings keys.
35-
<!-- TODO Add an example -->
34+
Added support for <link linkend="language.types.array.unpacking">array unpacking with strings keys</link>.
35+
<informalexample>
36+
<programlisting role="php">
37+
<![CDATA[
38+
<?php
39+
$arr1 = [1, 'a'];
40+
$arr2 = [...$arr1, 'c' => 'd']; //[1, 'a', 'c' => 'd']
41+
?>
42+
]]>
43+
</programlisting>
44+
</informalexample>
3645
</para>
3746
<!-- RFC: https://wiki.php.net/rfc/array_unpacking_string_keys -->
3847
</sect3>

language/types/array.xml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,100 @@ array(3) {
940940
</para>
941941
</sect2>
942942

943+
<sect2 xml:id="language.types.array.unpacking">
944+
<title>Array unpacking</title>
945+
946+
<para>
947+
An array prefixed by <code>...</code> will be expanded in place during the definition of the array.
948+
Only arrays and objects which implement <interfacename>Traversable</interfacename> can be expanded.
949+
Array unpacking with <code>...</code> is available as of PHP 7.4.0.
950+
</para>
951+
952+
<para>
953+
It's possible to expand multiple times, and add normal elements before or after the <code>...</code> operator:
954+
955+
<example>
956+
<title>Simple array unpacking</title>
957+
<programlisting role="php">
958+
<![CDATA[
959+
<?php
960+
// Using short array syntax.
961+
// Also, works with array() syntax.
962+
$arr1 = [1, 2, 3];
963+
$arr2 = [...$arr1]; //[1, 2, 3]
964+
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
965+
$arr4 = [...$arr1, ...$arr2, 111]; //[1, 2, 3, 1, 2, 3, 111]
966+
$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]
967+
968+
function getArr() {
969+
return ['a', 'b'];
970+
}
971+
$arr6 = [...getArr(), 'c' => 'd']; //['a', 'b', 'c' => 'd']
972+
?>
973+
]]>
974+
</programlisting>
975+
</example>
976+
</para>
977+
978+
<para>
979+
Unpacking an array with the <code>...</code> operator follows the semantics of the <function>array_merge</function> function.
980+
That is, later string keys overwrite earlier ones and integer keys are renumbered:
981+
982+
<example>
983+
<title>Array unpacking with duplicate key</title>
984+
<programlisting role="php">
985+
<![CDATA[
986+
<?php
987+
// string key
988+
$arr1 = ["a" => 1];
989+
$arr2 = ["a" => 2];
990+
$arr3 = ["a" => 0, ...$arr1, ...$arr2];
991+
var_dump($arr3); // ["a" => 2]
992+
993+
// integer key
994+
$arr4 = [1, 2, 3];
995+
$arr5 = [4, 5, 6];
996+
$arr6 = [...$arr4, ...$arr5];
997+
var_dump($arr6); // [1, 2, 3, 4, 5, 6]
998+
// Which is [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6]
999+
// where the original integer keys have not been retained.
1000+
?>
1001+
]]>
1002+
</programlisting>
1003+
</example>
1004+
</para>
1005+
1006+
<note>
1007+
<para>
1008+
Keys that are neither integers nor strings throw a <classname>TypeError</classname>.
1009+
Such keys can only be generated by a <interfacename>Traversable</interfacename> object.
1010+
</para>
1011+
</note>
1012+
<note>
1013+
<para>
1014+
Prior to PHP 8.1, unpacking an array which has a string key is not supported:
1015+
</para>
1016+
<informalexample>
1017+
<programlisting role="php">
1018+
<![CDATA[
1019+
<?php
1020+
1021+
$arr1 = [1, 2, 3];
1022+
$arr2 = ['a' => 4];
1023+
$arr3 = [...$arr1, ...$arr2];
1024+
// Fatal error: Uncaught Error: Cannot unpack array with string keys in example.php:5
1025+
1026+
$arr4 = [1, 2, 3];
1027+
$arr5 = [4, 5];
1028+
$arr6 = [...$arr4, ...$arr5]; // works. [1, 2, 3, 4, 5]
1029+
?>
1030+
]]>
1031+
</programlisting>
1032+
</informalexample>
1033+
</note>
1034+
1035+
</sect2>
1036+
9431037
<sect2 xml:id="language.types.array.examples">
9441038
<title>Examples</title>
9451039

0 commit comments

Comments
 (0)