PHP对象数组属性,如果它们不存在的话


php object array properties if they don't exist

首先,我想我已经尝试了所有我遇到的解决方案,但仍然有一个问题。

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
        )
)
上面的

是输出对象的一个示例。我在寻找细节,有些细节有时是不存在的。如果它们不存在,我只是想抓住事实标志它为"无"为我的DB和移动到下一个。对我来说,我就没那么幸运了。不管我怎么接近它,我都会遇到错误。

我是否执行isset() !isset() empty() !empty()或以上几种方法的组合来捕获它,如空、null、未定义、空白或甚至不存在

示例中,您将看到上面的示例输出中没有location->name对象。所以我的最新尝试是

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

我的错误是"未定义的属性:stdClass::$location",无论我如何尝试捕获它,我仍然得到同样的东西。

您需要先检查"location"属性,然后再深入查看

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

您可能需要查看Reflection API