JSON解析php问题

JSON解析php问题,php,json,parsing,Php,Json,Parsing,我有这个json响应 {"success":true,"results":[{"response":["random1"],"id":"6566","Limit":1},{"response":["random2"],"id":"6563","Limit":1},{"response":["random3"],"id":"6568","Limit":1}]} 我只需要从响应中提取随机数1,随机数2,随机数3,最终结果如下: 随机数1 随机数2 随机数3 我有这个剧本 $jsonData

我有这个json响应

 {"success":true,"results":[{"response":["random1"],"id":"6566","Limit":1},{"response":["random2"],"id":"6563","Limit":1},{"response":["random3"],"id":"6568","Limit":1}]}
我只需要从响应中提取随机数1,随机数2,随机数3,最终结果如下:

  • 随机数1
  • 随机数2
  • 随机数3
我有这个剧本

$jsonData = file_get_contents("json");

$json = json_decode($jsonData,true); 

foreach($json["results"][0]["response"] as $data) {
 {
   echo json_encode($data);

}
}
但这只会提取

  • 随机数1
如果我在
[1]
中更改
[0]
将提取随机数2和
[2]
随机数3 我怎样才能立即得到random1,random2,random3的响应?因此,最终的回音将是:

  • 随机数1
  • 随机数2
  • 随机数3

提前谢谢你,任何帮助都将不胜感激

在你的
$json['result']
循环你所做的只是将第一个索引
0
作为它的
响应

$json = '{"success":true,"results":[{"response":["random1"],"id":"6566","Limit":1},{"response":["random2"],"id":"6563","Limit":1},{"response":["random3"],"id":"6568","Limit":1}]}';
$json = json_decode($json,true); 

foreach($json["results"] as $data) {
   echo $data['response'][0]."\n";
}
试试看


更新

如果你想过滤,比如说
random1
,那么就这样做

$json = json_decode($json,true); 
$filter = array("random1"); //You can add items to filter
$result = array();
foreach($json["results"] as $data) {
   if(!in_array($data['response'][0],$filter))
       $result[] = $data;
}
print_r($result);

$result
将只包含
random2
random3
results
是一个数组,因此您可以通过以下方式获得数组中的第一个:
$json[“results”][0]

如果要迭代所有值,应如下所示:

foreach($json["results"] as $data) {
   echo json_encode($data['response']);

}

@没问题。很高兴我帮了忙。请确保接受答案以供将来参考,并帮助其他人解决相同的问题。是否更改在其中插入单词过滤器?所以我可以过滤一些我用if(strpos($data,'random1')!==false)尝试过的单词,但不会work@AlexandruVorobchevici什么样的话
random1
random3
?假设我想过滤random1。。因此最终结果将是随机2和随机3,使用foreach($json[“results”]作为$data)修复{如果(preg_match('/^(badword)$/i',$data['results'][0])继续;echo$data['answers'][0]。“\n”}再次感谢您的帮助!
foreach($json["results"] as $data) {    echo json_encode($data['response']);}