@@ -940,6 +940,100 @@ array(3) {
940
940
</para >
941
941
</sect2 >
942
942
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
+
943
1037
<sect2 xml : id =" language.types.array.examples" >
944
1038
<title >Examples</title >
945
1039
0 commit comments