PHP测试子对象的空值

时间:2013-05-10 16:31:03

标签: php json

我有一个json对象已被解码为:

item_json:stdClass Object
(
    [code] => 2013CA
    [SP] => stdClass Object
        (
            [PID] => 1175630
            [FirstName] => Kim
            [LastName] => Kardashian
            [DOB] => 01-02-1978
            [Address1] => 12345 Kardashian Way
            [Address2] => 
            [Address3] => 
            [City] => Hollywood
            [State] => CA
            [Zip] => 90210
            [Country] => US
            [Phone] => 1-210-5551212-
            [Email] => kkardashian@kkardashian.com
            [Info] => stdClass Object
                (
                    [Declined] => null
                    [NameOnLicense] => KK
                    [State] => CA
                    [License] => 90210
                    [LicenseText] => License Number
                    [TypeID] => 215057
                    [Hours] => 24
                    [Units] => 24
                    [Price] => 0
                )

        )

)

如何测试Info-> Declined是否为空?

我已经尝试了

!isset($item_json-SP->Info->Declined) 

$item_json-SP->Info->Declined == null

但他们都失败了。

2 个答案:

答案 0 :(得分:1)

试试这个

if ($item_json->SP->Info->Declined == null)

你在这里有一个错误$ item_json-SP它不是这个运算符,你有语法错误,PHP内置了检查空值

if(is_null($json->SP->Info->Declined)) {
}

答案 1 :(得分:1)

测试值NULL的首选方法是使用is_null()函数。请注意,Declined的正确路径为:SP->Info->Declined。试试这个:

if(is_null($json->SP->Info->Declined)) {

}