drgbpq5930 2012-03-13 03:32
浏览 7
已采纳

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

All, I'm using a function like this:

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

The line in the function is giving me the following error: Fatal error: Cannot use object of type stdClass as array

When I do a print_r on $tweet I get the following output:

object(stdClass)#70 (19) {
  ["in_reply_to_screen_name"]=>
  NULL
  ["in_reply_to_user_id"]=>
  NULL
  ["in_reply_to_status_id"]=>
  NULL
  ["coordinates"]=>
  NULL
  ["retweeted"]=>
  bool(false)
  ["created_at"]=>
  string(30) "Mon Mar 12 16:54:05 +0000 2012"
  ["id_str"]=>
  string(18) "12345553333"
  ["truncated"]=>
  bool(false)
  ["user"]=>
  object(stdClass)#78 (38) {
    ["id"]=>
    int(522392463)
    ["profile_image_url_https"]=>
    string(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"]=>
    string(6) "333333"
    ["is_translator"]=>
    bool(false)
    ["favourites_count"]=>
    int(0)
    ["location"]=>
    string(11) "Chicago, IL"
    ["time_zone"]=>
    NULL
    ["utc_offset"]=>
    NULL
    ["show_all_inline_media"]=>
    bool(false)
    ["profile_sidebar_border_color"]=>
    string(6) "C0DEED"
    ["name"]=>
    string(20) "name"
    ["protected"]=>
    bool(false)
    ["profile_background_image_url_https"]=>
    string(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_request_sent"]=>
    NULL
    ["default_profile"]=>
    bool(true)
    ["statuses_count"]=>
    int(2)
    ["description"]=>
    string(56) "Description"
    ["notifications"]=>
    NULL
    ["verified"]=>
    bool(false)
    ["profile_background_color"]=>
    string(6) "C0DEED"
    ["listed_count"]=>
    int(0)
    ["profile_image_url"]=>
    string(71) "http://a0.twimg.com/profile_images/1891637329/turntable_dots_normal.jpg"
    ["profile_background_image_url"]=>
    string(47) "http://a0.twimg.com/images/themes/theme1/bg.png"
    ["followers_count"]=>
    int(3)
    ["url"]=>
    string(28) "http://www.website.com"
    ["friends_count"]=>
    int(9)
    ["created_at"]=>
    string(30) "Mon Mar 12 16:38:11 +0000 2012"
    ["profile_link_color"]=>
    string(6) "0084B4"
    ["screen_name"]=>
    string(10) "Username"
  }
  ["in_reply_to_status_id_str"]=>
  NULL
  ["geo"]=>
  NULL
  ["retweet_count"]=>
  int(0)
  ["favorited"]=>
  bool(false)
  ["place"]=>
  NULL
  ["source"]=>
  string(3) "web"
  ["in_reply_to_user_id_str"]=>
  NULL
  ["contributors"]=>
  NULL
  ["id"]=>
  float(1.7924891374269E+17)
  ["text"]=>
  string(140) "This is the tweet"
}

How can I access the text without getting the error?

Thanks!

  • 写回答

2条回答 默认 最新

  • ds3422222 2012-03-13 03:34
    关注

    The error means exactly what it is, $tweet is not an array, thus it makes no sense trying to get its 0th index.

    $tweet->text should just be fine :)

    If you make sure $tweet is an array of tweet informations:

    foreach($tweet as $t) {
       echo $t->text;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用