2013-08-30 9 views
0

我在这里有第3条推文,但我一直有问题将它们保存到我的数组中。他们需要的是字符串,但我不断收到这样的:将来自Object的数据推入数组

Fatal error: Cannot use object of type stdClass as array in XXX/classes/page.class.php on line 117

$arrTweets = array(); 
foreach($tweets as $tweet) { 
    for ($i = 0; $i < 3; $i++) { 
     array_push($arrTweets, $tweet[$i] - > text); 
    } 
} 
+0

'$ tweets'是什么? –

回答

2

没有确切知道你的变量,如果你的设计是不错的,应该没有必要使用for循环:

$arrTweets = array(); 
foreach ($tweets as $tweet){ 
    array_push($arrTweets, $tweet->text); 
} 
0

如果您只想要前3条推文,请删除foreach并使用for循环。

$arrTweets = array(); 

     for($i = 0; $i < 3; $i++){ 
      array_push($arrTweets, $tweets[$i]->text); 
     }