JSON解析php问题
我有这个json响应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
{"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']);}