rfc:spread_operator_for_array
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:spread_operator_for_array [2018/10/28 03:29] – jhdxr | rfc:spread_operator_for_array [2019/04/05 06:09] – jhdxr | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Spread Operator in Array ====== | + | ====== Spread Operator in Array Expression |
- | * Version: 0.1 | + | * Version: 0.2 |
* Date: 2018-10-13 | * Date: 2018-10-13 | ||
* Author: CHU Zhaowei, jhdxr@php.net | * Author: CHU Zhaowei, jhdxr@php.net | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | PHP has already supported [[rfc: | + | PHP has already supported [[rfc: |
===== Proposal ===== | ===== Proposal ===== | ||
- | An array pair prefixed by ... will be expanded in places during array definition. Only arrays and objects who implement Traversable can be expanded. | + | An array pair prefixed by '' |
For example, | For example, | ||
Line 29: | Line 29: | ||
</ | </ | ||
- | It's possible to unpack arrays with string | + | ==== String |
- | For arrays with numeric keys, they will be renumbered with incrementing keys starting from zero. So if unpack multiple arrays with same numeric key, the later value will be appended instead of overwriting the previous one. | + | In order to make the behavior consistent |
- | + | ||
- | It's possible to unpack arrays | + | |
- | <code php> | + | |
- | $arr1 = [' | + | |
- | $arr2 = [11 => 11, 22 => 22, 33 => 33]; | + | |
- | $arr3 = [111 => 9, 22 => 8, ' | + | |
- | var_dump([...$arr1, ...$arr2]); | + | |
- | /* | + | |
- | array(6) { | + | |
- | ["a" | + | |
- | int(1) | + | |
- | [" | + | |
- | int(2) | + | |
- | [" | + | |
- | int(3) | + | |
- | [0]=> | + | |
- | int(11) | + | |
- | [1]=> | + | |
- | int(22) | + | |
- | [2]=> | + | |
- | int(33) | + | |
- | } | + | |
- | */ | + | |
- | var_dump([...$arr1, ...$arr3]); | + | |
- | /* | + | |
- | array(5) { | + | |
- | [" | + | |
- | int(1) | + | |
- | [" | + | |
- | int(2) | + | |
- | [" | + | |
- | int(7) | + | |
- | [0]=> | + | |
- | int(9) | + | |
- | [1]=> | + | |
- | int(8) | + | |
- | } | + | |
- | */ | + | |
- | var_dump([...$arr2, | + | |
- | /* | + | |
- | array(6) { | + | |
- | [0]=> | + | |
- | int(11) | + | |
- | [1]=> | + | |
- | int(22) | + | |
- | [2]=> | + | |
- | int(33) | + | |
- | [3]=> | + | |
- | int(9) | + | |
- | [4]=> | + | |
- | int(8) | + | |
- | [" | + | |
- | int(7) | + | |
- | } | + | |
- | */ | + | |
- | </ | + | |
==== By-reference passing ==== | ==== By-reference passing ==== | ||
Line 93: | Line 37: | ||
<code php> | <code php> | ||
$arr1 = [1, 2, 3]; | $arr1 = [1, 2, 3]; | ||
- | $arr2 = [...& | + | $arr2 = [...& |
</ | </ | ||
Line 116: | Line 60: | ||
</ | </ | ||
+ | ===== Backward Incompatible Changes ===== | ||
+ | This change should not break anything. | ||
+ | ===== Q & A ===== | ||
+ | ==== Advantages over array_merge ==== | ||
+ | - Spread operator should have a better performance than '' | ||
+ | - '' | ||
- | ===== Backward Incompatible Changes ===== | + | ==== ... should be preserved for other use (e.g. map concat) |
- | This change | + | This is kind of out of scope here to discuss other concat / merge operation. The important thing is we should |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== |
rfc/spread_operator_for_array.txt · Last modified: 2019/05/13 12:45 by nikic