如何解析嵌套的 JSON 数组


how to parse a nested json array

我有一个json如下,

{
   "responseHeader":{
      "status":0,
      "QTime":1
   },
   "spellcheck":{
      "suggestions":[
         "goo",
         {
            "numFound":5,
            "startOffset":0,
            "endOffset":3,
            "suggestion":[
               "good",
               "googl",
               "goodby",
               "goos",
               "goodwil"
            ]
         },
         "collation",
         "good"
      ]
   }
}

我尝试了下面的php代码来获取建议中的元素列表,即好,谷歌,再见,古斯,商誉

$myArray = json_decode($response, true);
foreach ($myArray['spellcheck']['suggestion'] as $doc) {
   echo $doc;
}

但是得到这个错误

Notice: Undefined index: suggestion in /Applications/XAMPP/xamppfiles/htdocs/ir/suggestions.php on line 9

如何获取建议的各个元素?

这只是

"建议"的拼写错误,您的数组索引缺少"s"(这是建议)

$myArray = json_decode($response, true);
foreach ($myArray['spellcheck']['suggestions'] as $doc) {
   echo $doc;
}