2017-09-21 179 views
2

我已经解析并循环遍历JSON很多次。我似乎用我以前没有做过的事情撞到墙上,我一直无法找到答案,并希望我正确地引用了这一点。我一直在为此苦苦挣扎。PHP - 通过索引JSON数组循环

通过下面的代码示例,我可以解析它并为对象指定一个变量,并像这样获取它的值。

$result = json_decode($json, true); 
print_r($result); 

echo $result['response'][0]['report']['type']; 

这是哪里出了问题进来,它与数组索引开始,我希望我指的是这个正确。基本上它是上面的[0]。我从来没有通过这样的循环,我的研究已经空了。通常我会这样做。

$json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}'; 
    $result = json_decode($json, true); 
    print_r($result);  

    foreach($result as report) { 

     $type = $report['report']['type']; 

    echo $type; 
    } 

这通常会在大多数情况下工作,但因为这是与每一个新的条目[0],并增加我不知道如何通过我的切入点循环不同的是什么,我已经处理之前并一直无法找到任何这样的工作示例。更不用说我不知道​​如何使用这些索引键将['response']加入到循环ether中。

这是一个数组的解码示例以及我正在使用的解码示例。

Array ([success] => 1 [error] => [response] => Array ([0] => Array ([id] => 59c30487db6be86b7f8b465a [loc] => Array ([long] => -90.45 [lat] => 43.43) [report] => Array ([code] => R [type] => heavy rain [name] => Gillingham [detail] => Array ([text] => 1 [rainIN] => 1 [rainMM] => 25.4) [reporter] => co-op observer [comments] => [timestamp] => 1505952240 [cat] => rain [dateTimeISO] => 2017-09-20T19:04:00-05:00 [datetime] => 2017-09-20T19:04:00-05:00 [wfo] => arx) [place] => Array ([name] => gillingham [state] => wi [county] => richland [country] => us) [profile] => Array ([tz] => America/Chicago)) [1] => Array ([id] => 59c302a9db6be82a758b46e8 [loc] => Array ([long] => -90.93 [lat] => 43.32) [report] => Array ([code] => R [type] => heavy rain [name] => Mount Sterling [detail] => Array ([text] => 2.25 [rainIN] => 2.25 [rainMM] => 57.15) [reporter] => trained spotter [comments] => [timestamp] => 1505951760 [cat] => rain [dateTimeISO] => 2017-09-20T18:56:00-05:00 [datetime] => 2017-09-20T18:56:00-05:00 [wfo] => arx) [place] => Array ([name] => mount sterling [state] => wi [county] => crawford [country] => us) [profile] => Array ([tz] => America/Chicago)) [2] => Array ([id] => 59c30106db6be8146c8b4661 [loc] => Array ([long] => -89.53 [lat] => 44.45) [report] => Array ([code] => R [type] => heavy rain [name] => Plover [detail] => Array ([text] => 1.05 [rainIN] => 1.05 [rainMM] => 26.67) [reporter] => co-op observer [comments] => One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail. [timestamp] => 1505950800 [cat] => rain [dateTimeISO] => 2017-09-20T18:40:00-05:00 [datetime] => 2017-09-20T18:40:00-05:00 [wfo] => grb) [place] => Array ([name] => plover [state] => wi [county] => portage [country] => us) [profile] => Array ([tz] => America/Chicago)) [3] => Array ([id] => 59c3080adb6be8d5138b4654 [loc] => Array ([long] => -89.33 [lat] => 44.37) [report] => Array ([code] => H [type] => hail [name] => 4 mi NNW Blaine [detail] => Array ([text] => 0.88 [hailIN] => 0.88 [hailMM] => 22.35) [reporter] => trained spotter [comments] => Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours. [timestamp] => 1505950680 [cat] => hail [dateTimeISO] => 2017-09-20T18:38:00-05:00 [datetime] => 2017-09-20T18:38:00-05:00 [wfo] => grb) [place] => Array ([name] => blaine [state] => wi [county] => portage [country] => us) [profile] => Array ([tz] => America/Chicago)) [4] => Array ([id] => 59c30106db6be8146c8b4660 [loc] => Array ([long] => -90.93 [lat] => 43.32) [report] => Array ([code] => H [type] => hail [name] => Mount Sterling [detail] => Array ([text] => 0.75 [hailIN] => 0.75 [hailMM] => 19.05) [reporter] => trained spotter [comments] => Hail ranged from 1/2 to 3/4 inch. [timestamp] => 1505950200 [cat] => hail [dateTimeISO] => 2017-09-20T18:30:00-05:00 [datetime] => 2017-09-20T18:30:00-05:00 [wfo] => arx) [place] => Array ([name] => mount sterling [state] => wi [county] => crawford [country] => us) [profile] => Array ([tz] => America/Chicago)) [5] => Array ([id] => 59c2fd80db6be844598b466d [loc] => Array ([long] => -89.77 [lat] => 44.21) [report] => Array ([code] => R [type] => heavy rain [name] => 6 mi ESE New Rome [detail] => Array ([text] => 4 [rainIN] => 4 [rainMM] => 101.6) [reporter] => trained spotter [comments] => [timestamp] => 1505949840 [cat] => rain [dateTimeISO] => 2017-09-20T18:24:00-05:00 [datetime] => 2017-09-20T18:24:00-05:00 [wfo] => arx) [place] => Array ([name] => rome [state] => wi [county] => adams [country] => us) [profile] => Array ([tz] => America/Chicago)) [6] => Array ([id] => 59c2f678db6be898338b4673 [loc] => Array ([long] => -89.3 [lat] => 44.46) [report] => Array ([code] => H [type] => hail [name] => Amherst Junction [detail] => Array ([text] => 1 [hailIN] => 1 [hailMM] => 25.4) [reporter] => trained spotter [comments] => [timestamp] => 1505948340 [cat] => hail [dateTimeISO] => 2017-09-20T17:59:00-05:00 [datetime] => 2017-09-20T17:59:00-05:00 [wfo] => grb) [place] => Array ([name] => amherst junction [state] => wi [county] => portage [country] => us) [profile] => Array ([tz] => America/Chicago)) [7] => Array ([id] => 59c2f2f6db6be8a2208b4672 [loc] => Array ([long] => -89.4 [lat] => 44.26) [report] => Array ([code] => H [type] => hail [name] => Almond [detail] => Array ([text] => 2 [hailIN] => 2 [hailMM] => 50.8) [reporter] => trained spotter [comments] => Report via social media [timestamp] => 1505948160 [cat] => hail [dateTimeISO] => 2017-09-20T17:56:00-05:00 [datetime] => 2017-09-20T17:56:00-05:00 [wfo] => grb) [place] => Array ([name] => almond [state] => wi [county] => portage [country] => us) [profile] => Array ([tz] => America/Chicago)))) 

我怎么会遍历这个与索引键[#],如果我指的这个错误随时纠正我。

+0

为什么向下票呢? –

回答

2

可以在$result["response"]使用循环为:

$json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}'; 
$result = json_decode($json, true); 

foreach($result["response"] as $report) { 
    $type = $report['report']['type']; 
    echo $type; 
} 

检查结果这里:http://sandbox.onlinephpfunctions.com/code/c7b55a67e649bdc8c35ec1ba8218747de1f8ac16

+1

为你的例子,你应该使用https://3v4l.org/,因为它更快,更清洁,并且支持https –

+0

哇,我觉得这很容易。这是我没有尝试的一件事。我确实尝试过,'$ result-> response'甚至不认为这样做。现在效果很好。 –

+0

@Jeff Puckett:谢谢你的建议。我将在未来的帖子中使用3v4l.org。 DFWStormForce:因为它是一个数组,不是一个对象,所以我们需要[]来访问。 –