开发者

php object array properties if they don't exist

First of I think I have tried all the solutions I have come across and still have an issue none the less..

facebookResponse Object
(
    [__construct:facebookResponse:private] => 
    [__resp] => stdClass Object
        (
            [data] => stdClass Object
                (
                    [id] => 00000000000
                    [name] => Random.User
                    [first_name] =>开发者_开发问答; Random
                    [last_name] => User
                    [link] => http://www.facebook.com/Random.User
                    [username] => Random.User
                    [birthday] => 11/10/1980
                    [gender] => male
                    [email] => xxxx@Random.User.com
                    [timezone] => -7
                    [locale] => en_US
                    [verified] => 1
                    [updated_time] => 2011-08-22T02:56:33+0000
                )

            [code] => 200
            [time] => 0.600512
            [length] => 386
            [type] => text/javascript; charset=UTF-8
        )

)

above is an example of the output object. I am looking for specifics, some of which sometimes don't exist. Where if they don't exist I just want to catch the fact flag it as "none" for my DB and move on to the next. For me Im not so lucky. no matter how I approach it I am running into errors..

whether i do isset() !isset() empty() !empty() or some combination of the above trying to catch it, as empty, null, undefined, blank, or just not even present.

Example you will see that there is no location->name object in the above sample output. So my latest attemt(s) to catch it as not there is

if((isset($fb_result->location->name))
    AND(!empty($fb_result->location->name))
   AND(trim($fb_result->location->name) !== ""))
   {
        $currenthome = $fb_result->location->name;
    }
   else
   {
    $currenthome = "none";
    }

my error with the above is "Undefined property: stdClass::$location" and no matter how I try to catch it, i still get the same thing.


You need to check for "location" attribute first, then go deeper

if(!empty($fb_result->location) && !empty($fb_result->location->name))
    $currenthome = $fb_result->location->name;
else
    $currenthome = 'none';


You might want to look at the Reflection API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜