JSOn内部子属性未定义索引错误


JSOn inner child attribute undefined index error

这是我的Json示例

{
   "164616":{
      "competitors":{
         "950053":{
            "Name":"Hookinn",
            "Jockey":"L PILLAR(A)",
            "DetailedPricing":{
               "winTotePlus":false,
               "placeTotePlus":false,
               "placePrices":{
                  "2":0,
                  "12":0
               },
               "RisaSilkID":1551
            }
         }
      }
   }
}

这是我的代码。

$string = file_get_contents(@"D:/test.json");
$json_a=json_decode($string,true);
    foreach ($json_a as $root_element => $childnode) {
        foreach( $childnode as $cKey => $subChild) {
            foreach( $subChild as $cKey2 => $subChild2) {
             echo($subChild2['Name']);  
         echo("<br>");
           foreach($subChild2['DetailedPricing'] as $compKey => $compVal) {
        //   echo($compVal['placeTotePlus']); echo("<br>");}
          }
        }
    }
}

我能够使用subChild2['Name']获得Name属性,但是当我试图访问DetailedPricing数组内的placeTotePlus属性时,我得到未定义的索引错误。我做错了什么?我还想获得placePrices的值。

谢谢

给你

$string = json_decode($string, true);
foreach($string as $item => $comp) {
    foreach($comp as $key =>$users) {
        foreach($users as $derp=>$user) {
            var_dump($user['DetailedPricing']['placeTotePlus']);
        }
    }
}

返回:

bool(false)

Example

不输出任何东西的原因是因为它是一个布尔值。您可以这样测试(如注释中所述):

(!$users['DetailedPricing']['placeTotePlus']) ? 'Its false' : 'Its true';

对于您的评论,您将使用 isset()

if(!isset($user['DetailedPricing']['PlaceTotPlus'])) {
    print 'nothing';
}