我試圖解析一個json文件到每個循環。問題在於數據嵌套在數量遞增的容器中,這是一個問題,因爲我不能僅僅抓住每個值。我花了一段時間試圖找到一種方法來讓這個工作,我已經空了。任何想法?解析每個增量的JSON PHP
這裏是JSON文件收拾,所以你可以明白我的意思 - http://www.jsoneditoronline.org/?url=http://ergast.com/api/f1/current/last/results.json
我想獲得的值,如[數字],但我也希望得到更深層次的價值觀,如[程序] [代碼]
<?php
// get ergast json feed for next race
$url = "http://ergast.com/api/f1/current/last/results.json";
// store array in $nextRace
$json = file_get_contents($url);
$nextRace = json_decode($json, TRUE);
$a = 0;
// get array for next race
// get date and figure out how many days remaining
$nextRaceDate = $nextRace['MRData']['RaceTable']['Races']['0']['Results'][' . $a++ . '];
foreach ($nextRaceDate['data'] as $key=>$val) {
echo $val['number'];
}
?>
json的其實並不重要。你已經解碼了它,所以它是一個普通的舊PHP數據結構,並且像任何其他PHP數據結構一樣迭代/處理它。如果你有嵌套結構,你需要多個嵌套循環。 –
您正在使用'$ a變量錯誤嘗試此操作:'$ nextRaceDate = $ nextRace ['MRData'] ['RaceTable'] ['Races'] ['0'] ['Results'] [$ a]' – cmnardi