不能将stdClass类型的对象用作数组


Cannot use object of type stdClass as array

全部,我使用的是这样一个函数:

function themeblvd_twitter_slider_default( $tweet, $options, $username ) {
    echo $tweet[0]->text;
}

函数中的行给出了以下错误:致命错误:无法将stdClass类型的对象用作数组

当我在$tweet上打印时,我会得到以下输出:

对象(stdClass)#70(19){["in_reply_to_screen_name"]=>NULL["in_reply_to_user_id"]=>NULL["in_reply_to_status_id"]=>NULL["坐标"]=>NULL["转发"]=>bool(false)["created_at"]=>string(30)"2012年3月12日星期一16:54:05+0000"["id_str"]=>string(18)"12345553333"["truncated"]=>bool(false)["user"]=>对象(stdClass)#78(38){["id"]=>int(522392463)["profile_image_url_https"]=>字符串(73)"https://si0.twimg.com/profile_images/1891637329/turntable_dots_normal.jpg"["contributors_enabled"]=>bool(false)["profile_use_background_image"]=>bool(true)["lang"]=>string(2)"en"["id_str"]=>string(9)"522392463"["default_profile_image"]=>bool(false)["geo_enabled"]=>bool(false)["profile_text_color"]=>字符串(6)"333333"["is_translator"]=>bool(false)["preferentes_count"]=>int(0)["location"]=>string(11)"伊利诺伊州芝加哥"["time_zone"]=>NULL["utc_offset"]=>NULL["show_all_inline_media"]=>bool(false)["profile_sidebar_border_color"]=>string(6)"C0DEED"["name"]=>string(20)"名称"["protected"]=>bool(false)["profile_background_image_url_https"]=>字符串(49)"https://si0.twimg.com/images/themes/theme1/bg.png"["profile_background_tile"=>bool(false)["following"]=>NULL["profile_sidebar_fill_color"=>string(6)"DDEEF6"["follow_requestrongent"]=>NULL["default_profile"]=>bool(true)["statuses_count"]=>int(2)["description"]=>string(56)"说明"["notifications"]=>NULL["已验证"]=>bool(false)["profile_background_color"]=>string(6)"C0DEED"["listed_count"]=>int(0)["profile_image_url"]=>字符串(71)"http://a0.twimg.com/profile_images/1891637329/turntable_dots_normal.jpg"["profile_background_image_url"=>字符串(47)"http://a0.twimg.com/images/themes/theme1/bg.png"["followers_count"]=>int(3)["url"]=>字符串(28)"http://www.website.com"["friends_count"]=>int(9)["created_at"]=>string(30)"2012年3月12日星期一16:38:11+0000"["profile_link_color"]=>字符串(6)"0084B4"["screen_name"]=>string(10)"用户名"}["in_reply_to_status_id_str"]=>NULL["geo"]=>NULL["retweet_count"]=>int(0)["收藏夹"]=>bool(false)["place"]=>NULL["source"]=>string(3)"web"["in_reply_to_user_id_str"=>NULL["贡献者"]=>NULL["id"]=>浮子(1.7924891374269E+17)["text"]=>string(140)"这是推特"}

如何访问文本而不出现错误?

谢谢!

该错误的确切含义是,$tweet不是数组,因此尝试获取其第0个索引毫无意义。

$tweet->text应该很好:)

如果你确定$tweet是一组tweet信息:

foreach($tweet as $t) {
   echo $t->text;
}

最简单的方法是将$tweet强制转换为数组:

$tweet = (array)$tweet;