2013-08-30 11 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); 
     }