If you are using PHP 7.4 or higher, you can use the distribution operator ... as the following examples from PHP Docs:
$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] function getArr() { return ['a', 'b']; } $arr6 = [...getArr(), 'c']; //['a', 'b', 'c'] $arr7 = [...new ArrayIterator(['a', 'b', 'c'])]; //['a', 'b', 'c'] function arrGen() { for($i = 11; $i < 15; $i++) { yield $i; } } $arr8 = [...arrGen()]; //[11, 12, 13, 14]
This works as in JavaScript ES6.
Read more at https://wiki.php.net/rfc/spread_operator_for_array .
Student of Sciences Jul 26 '19 at 17:25 2019-07-26 17:25
source share