如何使用foreach来回显数组中的描述?


How can I use foreach to echo the descriptions in my array?

我使用twitter输出来获取我的tweet,现在有一个数据数组,我想使用foreach循环显示所有描述项。我如何循环通过一个对象(stdclass)??

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

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

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

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

foreach还可以遍历对象,查找其可见属性。

简单的

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