Both sides previous revisionPrevious revision | Next revisionBoth sides next revision |
rfc:additional-splat-usage [2017/09/06 02:58] – Reference with title haskellcamargo | rfc:additional-splat-usage [2017/09/06 12:04] – Examples: Numeric, Associative and Multidimensional arrays haskellcamargo |
---|
The new syntax gives equivalent behaviour to the ''array_merge()'' example above, where the contents of ''$arr1'' are appended to the array literal, and stored in ''$arr2''. The same rules for merging are followed; in the case of duplicated string keys, the later value overwrites the earlier when read from left to right, and numerically indexed arrays are appropriately re-keyed and appended. Any number of variables can be unpacked at any position in the array literal, and may be combined with regular element declarations in any order. | The new syntax gives equivalent behaviour to the ''array_merge()'' example above, where the contents of ''$arr1'' are appended to the array literal, and stored in ''$arr2''. The same rules for merging are followed; in the case of duplicated string keys, the later value overwrites the earlier when read from left to right, and numerically indexed arrays are appropriately re-keyed and appended. Any number of variables can be unpacked at any position in the array literal, and may be combined with regular element declarations in any order. |
| |
As such, the following applications of this operator should be equivalent: | As such, the following application of ''array_merge()'' and of the splat operator and should be equivalent: |
<code php> | <code php> |
<?php | |
$array1 = ["color" => "red", "model" => "Corolla"]; | $array1 = ["color" => "red", "model" => "Corolla"]; |
$array2 = ["type" => "car", "year" => "2002"]; | $array2 = ["type" => "car", "year" => "2002"]; |
$result = array_merge($array1, $array2); | $result = array_merge($array1, $array2); |
$result2 = [...$array1, ...$array2]; | $result2 = [...$array1, ...$array2]; |
?> | |
</code> | </code> |
| |
Combinations of the splat operator in variable and in other literal should also work: | A mixture of applying the splat operator to previously assigned variables and other literal should work just as fine: |
<code php> | <code php> |
<?php | |
$array1 = ["color" => "red", "model" => "Corolla"]; | $array1 = ["color" => "red", "model" => "Corolla"]; |
$array2 = ["type" => "car", "year" => "2002"]; | $array2 = ["type" => "car", "year" => "2002"]; |
$result = array_merge($array1, ["category" => "suv", "condition" => "good"], $array2); | $result = array_merge($array1, ["category" => "suv", "condition" => "good"], $array2); |
$result2 = [...$array1, ...["category" => "suv", "condition" => "good"], ...$array2]; | $result2 = [...$array1, ...["category" => "suv", "condition" => "good"], ...$array2]; |
?> | |
</code> | </code> |
| |
| The operator should have the same behaviour for numeric and associative arrays. |
| Multidimensional arrays should not be flattened: the splat operator should have effect in the first level keys only. |
| |
| === Numeric === |
| |
| <code php> |
| $threetofive = [3,4,5]; |
| $zerotofive = [0, 1, 2, ...$threetofive]; |
| </code> |
| |
| === Associative === |
| |
| <code php> |
| $addressData = ["street" => "George St.", "number" => 2]; |
| $cityData = ["city" => "Brisbane", "postcode" => 4000]; |
| |
| $propertyData = ["name" => "QUT", ...$addressData, ...$cityData]; |
| </code> |
| |
| === Multidimensional === |
| |
| <code php> |
| $old_marks = [ |
| "john" => [ |
| "maths" => 0, |
| "english" => 8 |
| ], |
| "jane" => [ |
| "maths" => 10, |
| "english" => 7 |
| ], |
| "joe" => [ |
| "maths" => 0, |
| "english" => 8 |
| ] |
| ]; |
| |
| $new_marks = [ |
| "matthew" => [ |
| "maths" => 5, |
| "english" => 5 |
| ], |
| ...$old_marks |
| ]; /* array(4) { ["matthew"]=> ... ["john"]=> ... ["jane"]=> ... ["joe"]=> ... }*/ |
| </code> |
| |
| |
Using the splat operator with elements that are not arrays and not ''Traversable'' should cause an error. | Using the splat operator with elements that are not arrays and not ''Traversable'' should cause an error. |
* [[https://wiki.php.net/rfc/argument_unpacking|Argument unpacking RFC]] | * [[https://wiki.php.net/rfc/argument_unpacking|Argument unpacking RFC]] |
* [[https://wiki.php.net/rfc/variadics|Variadics RFC]] | * [[https://wiki.php.net/rfc/variadics|Variadics RFC]] |
* [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator|Spread Operator]] | * [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator]] |