2017-09-21 185 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:因爲它是一個數組,不是一個對象,所以我們需要[]來訪問。 –