$arr1 = [1, 2, 3]; $arr2 = [...$arr1]; // [1, 2, 3] $arr3 = [0, ...$arr1]; // [0, 1, 2, 3] $arr4 = array(...$arr1, ...$arr2, 111); // [1, 2, 3, 1, 2, 3, 111] $arr5 = [...$arr1, ...$arr1]; // [1, 2, 3, 1, 2, 3]
Parse error: syntax error, unexpected '...'(T_ELLIPSIS), expecting ']' in /app/spread-operator.php on line 3相反,PHP 7.4 将返回一个数组: array(5){[0]=>string(6)"banana"[1]=>string(6)"orange"[2]=>string(5)"apple"[3]=>string(4)"pear"[4]=>string(10)"watermelon"}RFC 声明...
从PHP 7.4开始,...可用于数组字面量展开: $array1 = [1,2]; $array2 = [3,4]; $result = [...$array1, ...$array2];//[1,2,3,4] AI代码助手复制代码 4. 生成器解包 ...可以解包生成器(Generator)中的值: functiongen(){yield1;yield2; }$array= [...gen()];// [1, 2] AI代...
array(5){[0]=>string(6)"banana"[1]=>string(6)"orange"[2]=>string(5)"apple"[3]=>string(4)"pear"[4]=>string(10)"watermelon"} RFC 声明我们可以多次扩展同一个数组。此外,我们可以在数组中的任何位置使用 Spread Operator 语法,因为可以在 spread 运算符之前或之后添加常规元素。因此,以下代码...
array(5) { [0]=> string(6) "banana" [1]=> string(6) "orange" [2]=> string(5) "apple" [3]=> string(4) "pear" [4]=> string(10) "watermelon" } RFC 声明我们可以多次扩展同一个数组。此外,我们可以在数组中的任何位置使用 Spread Operator 语法,因为可以在 spread 运算符之前或之后添...
Accumulates adult users. The$usersarray containsUserobjects with promoted properties. Thearray_reducefunction starts with an empty array ([]) as the initial$carryvalue. The callback uses a ternary operator: if$u->age >= 18, the current user is appended to$carryusing the spread operator (.....
I wished to point out that while other comments state that the spread operator should be faster than array_merge, I have actually found the opposite to be true for normal arrays. This is the case in both PHP 7.4 as well as PHP 8.0. The difference should be negligible for most application...
array(5) { [0]=>string(6)"banana "[1]=>string(6)"orange "[2]=>string(5)"apple "[3]=>string(4)"pear "[4]=>string(10)"watermelon"} RFC声明我们可以多次扩展同一个数组。此外,我们可以在数组中的任何位置使用Spread Operator语法,因为可以在spread运算符之前或之后添加常规元素。因此,以下代码...
$array1 = ['dog', 'cat']; $array2 = ['bird', 'fish']; $all = array_merge($array1, $array2, ['lizard', 'ant farm']); Using the spread operator means we can define an array and unpack other arrays into it; the following is equivalent to the previous: $all = [...$array...
三个点...叫做Spread运算符, 第一个好处是性能 Spread 运算符应该比 array_merge 拥有更好的性能。这不仅仅是 Spread 运算符是一个语法结构,而 array_merge 是一个方法。还是在编译时,优化了高效率的常量数组 $parts = ['apple', 'pear']; $fruits = ['banana', 'orange', ...$parts, 'watermelon'...