2011-12-15 47 views
0

我正在使用twitter ouath获取我的推文,现在有一组数据,我希望使用foreach循环显示所有说明项目。我如何循环对象(stdclass)?如何使用foreach来回显数组中的描述?

array(7) { 
[0]=> object(stdClass)#25 (19) { 
    ["geo"]=> NULL 
    ["truncated"]=> bool(false) 
    ["id_str"]=> string(18) "147102180648824832" 
    ["retweet_count"]=> int(0) 
    ["favorited"]=> bool(false) 
    ["in_reply_to_screen_name"]=> NULL 
    ["in_reply_to_user_id"]=> NULL 
    ["in_reply_to_status_id_str"]=> NULL 
    ["user"]=> object(stdClass)#26 (38) { 
    ["profile_use_background_image"]=> bool(false) 
    ["protected"]=> bool(false) 
    ["is_translator"]=> bool(false) 
    ["follow_request_sent"]=> bool(false) 
    ["following"]=> bool(false) 
    ["geo_enabled"]=> bool(false) 
    ["profile_text_color"]=> string(6) "333333" 
    ["name"]=> string(9) "User" 
    ["id_str"]=> string(9) "387634665" 
    ["statuses_count"]=> int(7) 
    ["verified"]=> bool(false) 
    ["profile_background_image_url_https"]=> string(49) "https://si0.twimg.com/images/themes/theme1/bg.png" 
    ["profile_background_image_url"]=> string(47) "http://a0.twimg.com/images/themes/theme1/bg.png" 
    ["favourites_count"]=> int(0) 
    ["show_all_inline_media"]=> bool(false) 
    ["utc_offset"]=> int(-10800) 
    ["profile_link_color"]=> string(6) "0084B4" 
    ["description"]=> string(158) "Our site is a simple helpful service designed to provide consultation to people seeking financial advice. We work with you, offering a friendly helping hand." 
    ["location"]=> string(9) "Worldwide" 
    ["time_zone"]=> string(9) "Greenland" 
    ["profile_background_color"]=> string(6) "fafafa" 
    ["url"]=> string(24) "http://www.domain.com" 
    ["profile_image_url_https"]=> string(69) "https://si0.twimg.com/profile_images/1579817273/icon-large_normal.png" 
    ["listed_count"]=> int(0) 
    ["contributors_enabled"]=> bool(false) 
    ["notifications"]=> bool(false) 
    ["profile_background_tile"]=> bool(false) 
    ["followers_count"]=> int(1) 
    ["profile_image_url"]=> string(67) "http://a0.twimg.com/profile_images/1579817273/icon-large_normal.png" 
    ["screen_name"]=> string(9) "User" 
    ["default_profile"]=> bool(false) 
    ["lang"]=> string(2) "en" 
    ["profile_sidebar_fill_color"]=> string(6) "DDEEF6" 
    ["created_at"]=> string(30) "Sun Oct 09 12:04:44 +0000 2011" 
    ["profile_sidebar_border_color"]=> string(6) "C0DEED" 
    ["id"]=> int(387634665) 
    ["default_profile_image"]=> bool(false) 
    ["friends_count"]=> int(1) } 
    ["coordinates"]=> NULL 
    ["retweeted"]=> bool(false) 
    ["in_reply_to_user_id_str"]=> NULL 
    ["place"]=> NULL 
    ["source"]=> string(3) "web" 
    ["id"]=> int(147102180648824832) 
    ["created_at"]=> string(30) "Wed Dec 14 23:54:27 +0000 2011" 
    ["contributors"]=> NULL 
    ["in_reply_to_status_id"]=> NULL 
    ["text"]=> string(7) "testing" 
} 
+1

可以清理用换行和漂亮缩进的数据? – evan 2011-12-15 02:00:24

回答

1

完全相同的方式循环访问数组,除了使用对象表示法来访问属性。

foreach($tweets as $tweet) { 
    echo $tweet->description; 
} 

foreach($tweets as $tweet) { 
    foreach($tweet as $field => $value) { 
    echo $field . ': ' . $value . '<br/>'; 
    } 
} 
0

这么简单

foreach($tweets as $tweet) { 
    echo $tweet->description; 
}