2017-08-16 42 views
0

这里是我的代码:如何将新项目推送到作为方法结果的数组中?

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

它抛出:

只有变量应参考

什么是错,我该如何修复它通过?

+1

'$ new_array = $这个 - >的twitter($要求); $ this-> results ['twitter'] = array_push($ new_array,“active”);' – JYoThI

+0

@JYoThI Thx。我不能这样做,对吗? – stack

+0

是的,你不能传递返回结果作为参数。所以你需要将它存储在变量中并作为参数传递。你可以通过引用一个函数来传递一个变量,这样函数可以修改变量 – JYoThI

回答

1

请注意array_push返回一个整数,表示加法后数组元素的计数。您可以使用array_merge代替:

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

谢谢,upvote。订单怎么样? 'active'会一直追加到数组的末尾吗? – stack

+0

该命令不应该是相关的..如果你真的需要一种方式来使用该状态,那么只需添加一个键如:array_merge($ this-> twitter($ request),['status'=>'active' ])..之后,你可以简单地使用它:'$ this-> results ['twitter'] ['status']' –

1

1:将结果存储在变量然后按

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

注:是你无法通过返回的结果作为参数。所以你需要将它存储在变量中并作为参数传递。您可以通过引用传递一个变量的函数,因此函数可以修改变量

1

也许你想是这样的:

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

这将Twitter的()函数的返回推入您阵列。 请注意,array_push只返回包含项目的数量。

+0

'返回的数组长度'active'吗? – stack

+0

这取决于您最终需要的阵列结构。 '$ newValue [$ this-> twitter()] =“active”; array_push($ this-> results ['twitter'],$ newValue);' – igi

相关问题