0

我有一个如下的json,

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

我尝试了下面的 php 代码来获取建议中的元素列表,即 good,google,goodby,goos,goodwill

$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

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

4

1 回答 1

1

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

$myArray = json_decode($response, true);
foreach ($myArray['spellcheck']['suggestions'] as $doc) {
   echo $doc;
}
于 2013-11-14T23:17:11.827 回答