未定义访问stdClass的变量错误但有效

时间:2011-08-12 22:31:35

标签: php codeigniter object

我正在制作一个Twitter应用程序,并且有一个像下面这样的对象数组。 我在尝试访问[status]内的数据时遇到了麻烦而没有收到错误。

我正在将我真正想要的细节填充到数组中。 到目前为止,第一级的工作正常

e.g。 $filtered_info[$key_id]['screen_name'] = $info->screen_name;

但是当涉及访问[status]中的数据时,如下所示:

$filtered_info[$key_id]['status'] = $info->status->text;

我收到错误(即使它确实运行)。所以我开始在另一个foreach中访问它,就像你在下面看到的那样但是当我这样做时,一些结果就消失了。可能并非所有实例都存在[状态]。这有点奇怪,这会删除整个人的信息。

foreach ($friends_info as $key => $hundred){

        foreach ($hundred as $friend => $info){

            $key_id = ($friend)+(100*$key);

            $filtered_info[$key_id]['screen_name'] = $info->screen_name;
            $filtered_info[$key_id]['profile_image_url'] = $info->profile_image_url;
            $filtered_info[$key_id]['followers'] = $info->followers_count;
            $filtered_info[$key_id]['tweet_count'] = $info->statuses_count;
            $filtered_info[$key_id]['status'] = $info->status->text;

            if(isset($info->status))
            {
                foreach ($info as $key => $value){
                    if($key == 'status')
                    {       
                        $filtered_info[$key_id]['created_at'] = date('Y-m-d H:i:s', strtotime($value->created_at));
                        $filtered_info[$key_id]['tweet'] = $value->text;
                        $filtered_info[$key_id]['in_reply_to_screen_name'] = $value->in_reply_to_screen_name ;
                    }
                }

            }else{
                $filtered_info[$key_id]['created_at'] = '0000-00-00 00:00:00';
                $filtered_info[$key_id]['tweet'] = 'NEVER';
                $filtered_info[$key_id]['in_reply_to_screen_name'] = 'NEVER';
            }
        }
}

foreach ($hundred as $friend => $info){ $key_id = ($friend)+(100*$key); $filtered_info[$key_id]['screen_name'] = $info->screen_name; $filtered_info[$key_id]['profile_image_url'] = $info->profile_image_url; $filtered_info[$key_id]['followers'] = $info->followers_count; $filtered_info[$key_id]['tweet_count'] = $info->statuses_count; $filtered_info[$key_id]['status'] = $info->status->text; if(isset($info->status)) { foreach ($info as $key => $value){ if($key == 'status') { $filtered_info[$key_id]['created_at'] = date('Y-m-d H:i:s', strtotime($value->created_at)); $filtered_info[$key_id]['tweet'] = $value->text; $filtered_info[$key_id]['in_reply_to_screen_name'] = $value->in_reply_to_screen_name ; } } }else{ $filtered_info[$key_id]['created_at'] = '0000-00-00 00:00:00'; $filtered_info[$key_id]['tweet'] = 'NEVER'; $filtered_info[$key_id]['in_reply_to_screen_name'] = 'NEVER'; } } }

stdClass Object ( [0] => Array (

            [0] => stdClass Object
                (
                    [notifications] => 
                    [profile_link_color] => 0077cc
                    [protected] => 
                    [is_translator] => 
                    [default_profile] => 
                    [statuses_count] => 11277
                    [favourites_count] => 830
                    [profile_sidebar_border_color] => ebebeb
                    [followers_count] => 438739
                    [name] => Lonely Planet
                    [follow_request_sent] => 
                    [time_zone] => Pacific Time (US & Canada)
                    [verified] => 
                    [profile_use_background_image] => 1
                    [utc_offset] => -28800
                    [profile_image_url] => http://a0.twimg.com/profile_images/1331339147/LP_Logo_Avatar_-_128_x_128_normal.JPG
                    [description] => Tweeting (& retweeting) the best in travel. 1. Follow us. 2. Tag your tweets with #lp 3. We read your posts 4. We re-tweet the best of them
                    [profile_background_color] => 061128
                    [lang] => en
                    [profile_background_image_url] => http://a2.twimg.com/profile_background_images/274400859/Background_-_1900_x_1200_-_Balloons.jpg
                    [screen_name] => lonelyplanet
                    [status] => stdClass Object
                        (
                            [possibly_sensitive] => 
                            [retweeted_status] => stdClass Object
                                (
                                    [possibly_sensitive] => 
                                    [in_reply_to_user_id] => 
                                    [truncated] => 
                                    [in_reply_to_status_id] => 
                                    [favorited] => 
                                    [in_reply_to_status_id_str] => 
                                    [place] => 
                                    [geo] => 
                                    [coordinates] => 
                                    [in_reply_to_screen_name] => 
                                    [in_reply_to_user_id_str] => 
                                    [source] => HootSuite
                                    [id_str] => 102098302245416960
                                    [contributors] => 
                                    [retweeted] => 
                                    [id] => 1.0209830224542E+17
                                    [retweet_count] => 9
                                    [text] => Franz Josef Glacier Helicopter Flight Experience and Video http://ht.ly/61wAh #travel #lp #ttot #FriFotos
                                    [created_at] => Fri Aug 12 19:25:06 +0000 2011
                                )

                            [in_reply_to_user_id] => 
                            [truncated] => 
                            [in_reply_to_status_id] => 
                            [favorited] => 
                            [in_reply_to_status_id_str] => 
                            [place] => 
                            [geo] => 
                            [coordinates] => 
                            [in_reply_to_screen_name] => 
                            [in_reply_to_user_id_str] => 
                            [source] => HootSuite
                            [id_str] => 102105858502176769
                            [contributors] => 
                            [retweeted] => 
                            [id] => 1.0210585850218E+17
                            [retweet_count] => 9
                            [text] => RT @LandLopers: Franz Josef Glacier Helicopter Flight Experience and Video http://ht.ly/61wAh #travel #lp #ttot #FriFotos
                            [created_at] => Fri Aug 12 19:55:08 +0000 2011
                        )

                    [listed_count] => 13033
                    [location] => Melbourne, Oakland, London
                    [contributors_enabled] => 
                    [profile_text_color] => 333333
                    [profile_background_image_url_https] => https://si0.twimg.com/profile_background_images/274400859/Background_-_1900_x_1200_-_Balloons.jpg
                    [profile_sidebar_fill_color] => dfe7f2
                    [url] => http://www.lonelyplanet.com
                    [id_str] => 15066760
                    [show_all_inline_media] => 1
                    [geo_enabled] => 1
                    [profile_background_tile] => 
                    [friends_count] => 402423
                    [profile_image_url_https] => https://si0.twimg.com/profile_images/1331339147/LP_Logo_Avatar_-_128_x_128_normal.JPG
                    [id] => 15066760
                    [default_profile_image] => 
                    [following] => 1
                    [created_at] => Mon Jun 09 22:53:07 +0000 2008
    )
所以,如果你能分享他们的智慧,那将是伟大的! 提前致谢, 杰奎

0 个答案:

没有答案