数组声明中的PHP扩展操作符

时间:2021-10-24 01:44:06

PHP supports the spread operator for variadic functions.

PHP支持变量函数的扩展运算符。

In JavaScript, you can use the spread operator to do this:

在JavaScript中,您可以使用扩展操作符来完成以下操作:

var a = [1, 2];
var b = [...a, 3, 4];
console.log(b); // [1, 2, 3, 4]

However, trying to do this in PHP:

但是,尝试在PHP中这样做:

$a = [1, 2];
$b = [...$a, 3, 4];
var_dump($b);die;

Results in this error:

导致这个错误:

Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']'

语法错误,意外的…”(T_ELLIPSIS),期待“]”

Is using the spread operator this way not allowed in PHP? If so, is there an equally-as-elegant way to achieve the same effect?

在PHP中不允许使用扩展操作符吗?如果是这样,是否有一种同样优雅的方式来达到同样的效果?

1 个解决方案

#1


3  

First of all you are referencing the Variadic function with arrays in wrong sense.

首先,用数组引用变量函数是错误的。

You can create your own method for doing this, or you can better use array_merge as suggested by @Mark Baker in comment under your question.

您可以为此创建自己的方法,或者您可以更好地使用array_merge,如@Mark Baker在您的问题下的注释中建议的那样。

If you still want to use spread operator / ..., you can implement something like this yourself.

如果您还想使用扩展操作符/…您可以自己实现类似的操作。

<?php
function merge($a, ...$b) {
    return array_merge($a,$b);
}

$a = [1, 2];
$b = [3,4];
print_r( merge($a, ...$b));
?>

But to me, doing it like this is stupidity. Because you still have to use something like array_merge. Even if a language implements this, behind the scene the language is using merge function which contains code for copying all the elements of two arrays into a single array. I wrote this answer just because you asked way of doing this, and elegancy was your demand.

但对我来说,这样做是愚蠢的。因为你仍然需要使用array_merge之类的东西。即使一种语言实现了这一点,在后台语言也使用merge函数,该函数包含将两个数组的所有元素复制到一个数组中的代码。我写这个答案只是因为你问我怎么做,而优雅是你的要求。

More reasonable example:

更合理的例子:

<?php
$a = [1,2,3,56,564];
$result = merge($a, 332, 232, 5434, 65);
var_dump($result);
?>

#1


3  

First of all you are referencing the Variadic function with arrays in wrong sense.

首先,用数组引用变量函数是错误的。

You can create your own method for doing this, or you can better use array_merge as suggested by @Mark Baker in comment under your question.

您可以为此创建自己的方法,或者您可以更好地使用array_merge,如@Mark Baker在您的问题下的注释中建议的那样。

If you still want to use spread operator / ..., you can implement something like this yourself.

如果您还想使用扩展操作符/…您可以自己实现类似的操作。

<?php
function merge($a, ...$b) {
    return array_merge($a,$b);
}

$a = [1, 2];
$b = [3,4];
print_r( merge($a, ...$b));
?>

But to me, doing it like this is stupidity. Because you still have to use something like array_merge. Even if a language implements this, behind the scene the language is using merge function which contains code for copying all the elements of two arrays into a single array. I wrote this answer just because you asked way of doing this, and elegancy was your demand.

但对我来说,这样做是愚蠢的。因为你仍然需要使用array_merge之类的东西。即使一种语言实现了这一点,在后台语言也使用merge函数,该函数包含将两个数组的所有元素复制到一个数组中的代码。我写这个答案只是因为你问我怎么做,而优雅是你的要求。

More reasonable example:

更合理的例子:

<?php
$a = [1,2,3,56,564];
$result = merge($a, 332, 232, 5434, 65);
var_dump($result);
?>