php array spread operator

Solutions on MaxInterview for php array spread operator by the best coders in the world

showing results for - "php array spread operator"
Lola
24 Sep 2018
1$arr1 = [1, 2, 3];
2$arr2 = [...$arr1]; // [1, 2, 3]
3$arr3 = [0, ...$arr1]; // [0, 1, 2, 3]
4$arr4 = array(...$arr1, ...$arr2, 111); // [1, 2, 3, 1, 2, 3, 111]
5$arr5 = [...$arr1, ...$arr1]; // [1, 2, 3, 1, 2, 3]
6
7function getArr() {
8  return ['a', 'b'];
9}
10$arr6 = [...getArr(), 'c']; // ['a', 'b', 'c']
11
12$arr7 = [...new ArrayIterator(['a', 'b', 'c'])]; // ['a', 'b', 'c']
13
14function arrGen() {
15    for($i = 11; $i < 15; $i++) {
16        yield $i;
17    }
18}
19$arr8 = [...arrGen()]; // [11, 12, 13, 14]
20
21
Hazel
10 May 2017
1$parts = ['apple', 'pear'];
2$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
3// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
4//////////////////////////////////////////////
5$arr1 = [1, 2, 3];
6$arr2 = [...$arr1]; // [1, 2, 3]
7$arr3 = [0, ...$arr1]; // [0, 1, 2, 3]
8$arr4 = array(...$arr1, ...$arr2, 111); // [1, 2, 3, 1, 2, 3, 111]
9$arr5 = [...$arr1, ...$arr1]; // [1, 2, 3, 1, 2, 3]
10function getArr() {
11  return ['a', 'b'];
12}
13$arr6 = [...getArr(), 'c']; // ['a', 'b', 'c']
14$arr7 = [...new ArrayIterator(['a', 'b', 'c'])]; // ['a', 'b', 'c']
15function arrGen() {
16    for($i = 11; $i < 15; $i++) {
17        yield $i;
18    }
19}
20$arr8 = [...arrGen()]; // [11, 12, 13, 14]