2013-05-10 31 views
0

我已解碼的JSON對象:PHP測試的子對象爲空值

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] => [email protected] 
      [Info] => stdClass Object 
       (
        [Declined] => null 
        [NameOnLicense] => KK 
        [State] => CA 
        [License] => 90210 
        [LicenseText] => License Number 
        [TypeID] => 215057 
        [Hours] => 24 
        [Units] => 24 
        [Price] => 0 
       ) 

     ) 

) 

如何測試,看看是否信息 - >拒絕爲空?

我已經試過

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

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

,但他們都失敗。

+0

他們是如何失敗的?你期望會發生什麼,發生了什麼? – 2013-05-10 16:32:26

+0

其實,我走了另一條路線,我真的需要檢查NameOnLicense是否爲空。我正在刪除這個問題。 – MB34 2013-05-10 16:44:31

+0

我真的希望SO會發表一條評論通知,指出倒計時帖子的人。躲在你的「匿名」背後並不酷。 – MB34 2013-05-10 20:35:40

回答

1

測試值是否爲NULL的首選方法是使用is_null()函數。此外請注意,正確的路徑爲Declined,其:SP->Info->Declined。試試這個:

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

} 
1

試試這個

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

您有一個錯誤在這裏$ item_json-SP是不是這樣操作,你有語法錯誤,有內置在PHP檢查空值

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