如何在php中循环遍历对象

18

我对对象不太熟悉,通常会使用脚本将它们转换为数组,但是现在我想要勇敢地学习并且想知道如何遍历一个对象数组并提取我需要的特定信息。下面是返回结果的示例。

[2] => stdClass Object
        (
            [id_str] => 152240682071900161
            [in_reply_to_status_id] => 
            [contributors] => 
            [place] => 
            [truncated] => 
            [geo] => 
            [favorited] => 
            [created_at] => Thu Dec 29 04:13:01 +0000 2011
            [user] => stdClass Object
                (
                    [id_str] => 71876190
                    [contributors_enabled] => 
                    [lang] => en
                    [protected] => 
                    [url] => http://www.imdb.com/name/nm0000362/
                    [default_profile] => 1
                    [profile_use_background_image] => 1
                    [name] => Danny DeVito
                    [default_profile_image] => 
                    [friends_count] => 14
                    [profile_text_color] => 333333
                    [statuses_count] => 5361
                    [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png
                    [utc_offset] => -28800
                    [description] => I'm an actor, director and producer. 
                    [is_translator] => 
                    [created_at] => Sat Sep 05 19:44:48 +0000 2009
                    [profile_link_color] => 0084B4
                    [following] => 1
                    [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png
                    [favourites_count] => 8
                    [follow_request_sent] => 
                    [geo_enabled] => 1
                    [profile_background_color] => C0DEED
                    [profile_background_tile] => 
                    [followers_count] => 1784732
                    [profile_image_url] => http://a2.twimg.com/profile_images/400232403/Photo_6_normal.jpg
                    [screen_name] => DannyDeVito
                    [show_all_inline_media] => 
                    [profile_sidebar_fill_color] => DDEEF6
                    [profile_image_url_https] => https://si0.twimg.com/profile_images/400232403/Photo_6_normal.jpg
                    [listed_count] => 20918
                    [notifications] => 
                    [location] => Los Angeles
                    [id] => 71876190
                    [verified] => 1
                    [time_zone] => Pacific Time (US & Canada)
                    [profile_sidebar_border_color] => C0DEED
                )

            [in_reply_to_user_id] => 
            [retweet_count] => 10
            [in_reply_to_screen_name] => 
            [in_reply_to_status_id_str] => 
            [possibly_sensitive] => 
            [retweeted] => 
            [source] => WhoSay
            [in_reply_to_user_id_str] => 
            [coordinates] => 
            [id] => 1.52240682072E+17
            [text] => #Trollfoot Maroma http://t.co/4MvCYIFe
        )

    [3] => stdClass Object
        (
            [id_str] => 152240419344879616
            [in_reply_to_status_id] => 
            [contributors] => 
            [place] => 
            [truncated] => 
            [geo] => 
            [favorited] => 
            [created_at] => Thu Dec 29 04:11:59 +0000 2011
            [user] => stdClass Object
                (
                    [id_str] => 71876190
                    [default_profile] => 1
                    [contributors_enabled] => 
                    [lang] => en
                    [protected] => 
                    [url] => http://www.imdb.com/name/nm0000362/
                    [profile_use_background_image] => 1
                    [name] => Danny DeVito
                    [default_profile_image] => 
                    [friends_count] => 14
                    [profile_text_color] => 333333
                    [statuses_count] => 5361
                    [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png
                    [utc_offset] => -28800
                    [description] => I'm an actor, director and producer. 
                    [is_translator] => 
                    [created_at] => Sat Sep 05 19:44:48 +0000 2009
                    [profile_link_color] => 0084B4
                    [following] => 1
                    [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png
                    [favourites_count] => 8
                    [follow_request_sent] => 
                    [geo_enabled] => 1
                    [profile_background_color] => C0DEED
                    [profile_background_tile] => 
                    [followers_count] => 1784725
                    [profile_image_url] => http://a2.twimg.com/profile_images/400232403/Photo_6_normal.jpg
                    [screen_name] => DannyDeVito
                    [show_all_inline_media] => 
                    [profile_sidebar_fill_color] => DDEEF6
                    [profile_image_url_https] => https://si0.twimg.com/profile_images/400232403/Photo_6_normal.jpg
                    [listed_count] => 20918
                    [notifications] => 
                    [location] => Los Angeles
                    [id] => 71876190
                    [verified] => 1
                    [time_zone] => Pacific Time (US & Canada)
                    [profile_sidebar_border_color] => C0DEED
                )

            [in_reply_to_user_id] => 
            [retweet_count] => 15
            [in_reply_to_screen_name] => 
            [in_reply_to_status_id_str] => 
            [possibly_sensitive] => 
            [retweeted] => 
            [source] => WhoSay
            [in_reply_to_user_id_str] => 
            [coordinates] => 
            [id] => 1.52240419345E+17
            [text] => #Trolltoll Maroma http://t.co/Egv9syon
        )

你可以展示一下你尝试过的代码吗?“你需要的具体信息”到底是什么? - Matt Ball
比如说,我只想获取每个对象的id_str。 - mcbeav
1个回答

39

你不需要将它们转换为任何东西。

在通用情况下,使用 foreach 循环即可:

foreach ($objects as $obj) {
   echo $obj->property;
}

对于内部对象,这将起作用:

foreach ($objects as $obj){
      echo $obj->user->description;
}

1
谢谢,我会试一下的。我以前从未尝试过,而且一直以为对象和数组的工作方式不同。 - mcbeav
7
实际上相反:foreach ($objects as $obj)。(译者注:该句英文原文已经是非常简洁明了的语言,因此这里直接翻译即可) - Luca Fagioli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接