如何遍历PHP中的对象 - php

我对对象不是很熟悉,我通常使用脚本将它们转换为数组,但是我认为是该动手学习这一知识的时候了,所以我问我如何遍历对象数组并提取特定信息我需要,下面是返回的示例。

[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
        )

参考方案

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

在泛型中,foreach循环可以很好地与您配合使用,如下所示:

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

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

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

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

PHP:填写数组中的“空白” - php

我有一个php数组(通过表单中的复选框值获取-您知道复选框仅在未设置时显示在_POST变量中)。Array ( [2] => 0,2 [3] => 0,3 ) 我需要一种方法来“填补” 0-5之间的差距。所以上面看起来像(用'-1'填充空白。我尝试用填充有'-1'的数组array_merge(),但这没用。Array ( [0] => -1…

PHP:对数组排序 - php

请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…

使用jquery计算<id>中的总图像 - php

我有一个div,里面有一些图像,例如<img src= etc我想计算该div中的图像总数。我也想使用PHP将这些图像ID保存到mysql中...谢谢 参考方案 要计算它们,您可以执行以下操作:alert($('#myDiv img').length); 要将所有id捕获到数组中,可以执行以下操作:var ids = []; $(&…

PHP:检查谁看过发送的电子邮件? - php

我正在向某些用户发送电子邮件,并且想知道是谁阅读的,这意味着如果有人阅读了该电子邮件,则将维护一个日志文件,其中包含该用户的电子邮件地址以及日期/时间/ IP。为此,我发送一个带有电子邮件(html模板)的javascript函数,当用户打开该电子邮件时,它仅会警告用户的电子邮件地址,例如:for($n=0; $n<sizeof($checkBox);…