2012-03-13 72 views
1

所有, 我使用這樣的功能:不能使用類型爲stdClass的對象作爲數組

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

函數中的線是給我下面的錯誤: 致命錯誤:無法使用的對象鍵入stdClass的作爲陣列

當我做$鳴叫我得到以下輸出的print_r:

 
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" 
} 

如何訪問沒有得到錯誤的文本?

謝謝!

+0

您正在調用不存在的'$ tweet [0]'... – Jon 2012-03-13 03:35:00

回答

4

這個錯誤的意思正是它是什麼,$tweet不是一個數組,因此它試圖獲得它的第0個索引是沒有意義的。

$tweet->text應該只是罰款:)

如果你確保$鳴叫是推特信息的數組:

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

如果我有更多的推文,那該怎麼看? – user1048676 2012-03-13 03:35:54

+0

編輯了我的答案 – 2012-03-13 03:38:10

2

最簡單方法是投$tweet作爲數組:

$tweet = (array)$tweet; 
相關問題