我试图在foreach循环中返回解码的JSON值。它可以工作,但是当我将其更改为不同的JSON URL源时,它不会返回任何数据。我知道我已经用[0]
结束了$raceDetails
,因为我想在每个增量中直接访问数据。我已经尝试指定数据更深一层的Races[0]->Circuit
,它仍然不返回任何数据。任何帮助都将非常感激。
$url = "http://ergast.com/api/f1/current/last/results.json";
$json = file_get_contents($url);
$nextRace = json_decode($json);
$raceDetails = $nextRace->MRData->RaceTable->Races[0]->Results;
foreach($raceDetails as $race){
echo '<tr>';
echo '<th class="r-pos">' . $race->position . '</th>';
echo '</tr>';
}
$url = "http://ergast.com/api/f1/current/last/results.json";
$json = file_get_contents($url);
$nextRace = json_decode($json);
$raceDetails = $nextRace->MRData->RaceTable->Races[0]->Results;
foreach($raceDetails as $race){
echo '<tr>';
echo '<th class="r-pos">' . $race->position . '</th>';
echo '</tr>';
}
破碎的代码 $url = "http://ergast.com/api/f1/2016.json";
$json = file_get_contents($url);
$nextRace = json_decode($json);
$raceDetails = $nextRace->MRData->RaceTable->Races[0];
foreach($raceDetails as $race){
echo '<tr>';
echo '<th class="r-pos">' . $race->Circuit->date . '</th>';
echo '</tr>';
}
$url = "http://ergast.com/api/f1/2016.json";
$json = file_get_contents($url);
$nextRace = json_decode($json);
$raceDetails = $nextRace->MRData->RaceTable->Races[0];
foreach($raceDetails as $race){
echo '<tr>';
echo '<th class="r-pos">' . $race->Circuit->date . '</th>';
echo '</tr>';
}
必须是
$raceDetails = $nextRace->MRData->RaceTable->Races;
foreach($raceDetails as $race){
echo '<tr>';
echo '<th class="r-pos">' . $race->date . '</th>';
echo '</tr>';
}