如何将新项目推送到作为方法结果的数组?

How can I push an new item to an array which is the result of a method?

这是我的代码:

$this->results['twitter'] = array_push($this->twitter($request),"active");

它抛出:

Only variables should be passed by reference

出了什么问题,我该如何解决?

1st : 将结果存储在变量中然后推送

$new_array = $this->twitter($request);
$this->results['twitter'] = array_push($new_array,"active");

注意: 是的,您不能将 return 结果作为参数传递。所以你需要将它存储在变量中并作为参数传递。您可以通过引用将变量传递给函数,以便函数可以修改变量

也许你想要这样的东西:

array_push($this->results['twitter'], $this->twitter());

这会将 twitter() 函数的 return 推送到您的数组中。 请注意 array_push 只有 return 包含项目的数量。

请注意,array_push returns 是一个整数,表示相加后数组元素的个数。您可以使用 array_merge 代替:

$this->results['twitter'] = array_merge($this->twitter($request), ['active']);