将来自Object的数据推送到数组中


Pushing data coming from Object into an array

我这里有前3条tweet,但我在将它们保存到数组中时一直遇到问题。它们需要是字符串,但我一直得到这个:

致命错误:无法将stdClass类型的对象用作中的数组第117行的XXX/class/page.class.php

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

在不知道所有变量的情况下,如果您的设计不错,就不需要使用for循环:

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

如果只想要前3个tweet,请删除foreach并使用for循环。

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