我从JSON API获取Pokemon数据,它显示任何Pokemon的值。这里的目的是获取数据,显示可以找到哪些游戏的口袋妖怪以及它们在游戏中找到的区域。
https://pokeapi.co/api/v2/pokemon/183/
可通过其encounters键值访问。我对访问途径特别感兴趣:
['location_area'] ['name']和['version'] ['name']
作为一个很好的网址示例:https://pokeapi.co/api/v2/pokemon/183/encounters
问题是,遇到数组索引值是一个URL,我通过自己的Curl json函数访问它,它工作正常,但只给我数组数字索引值。
public function getPokemonLocation($searchTerm)
{
/*
Get the Pokemon via search term name
*/
$url = $this->baseUrl.$searchTerm;
$pokemonLocation = $this->getUrl($url);
/*
$pokemonLocationUrl is the encounters subset i the array URL
*/
$pokemonLocationUrl = $pokemonLocation['location_area_encounters'];
$pokemonLocationEncounters = $this->getUrl('https://pokeapi.co/' . $pokemonLocationUrl);
echo "<pre>";
print_r($pokemonLocationEncounters);
echo "</pre>";
/*
Now grab the data needed from the name of location and what game regions they are from
*/
$pokemonAreaEncounterArea = array();
$pokemonAreaEncounterGame = array();
foreach($pokemonLocationEncounters as $key => $encounter)
{
$pokemonAreaEncounterArea[] = $encounter['location_area']['name'];
$pokemonAreaEncounterGame[] = $encounter['version_details'][0]['version']['name'];
}
$pokemonLocationAndRegion = array_combine($pokemonAreaEncounterGame,$pokemonAreaEncounterArea);
// print_r($pokemonLocationAndRegion);
return $pokemonLocationAndRegion;
}
问题是,遇到数组索引值是一个URL,我通过自己的Curl json函数访问它,它工作正常,但只给我数组数字索引值。
我可以轻松获取数据,如上所示。 Howwever想要完全访问所有数组索引,而不仅仅是一个或少数几个数据。很想通过多个foreach来做到这一点,但宁愿干净地实现它。
编辑:这是上面的Poke APi链接的第二个例子,如果你得到504服务器错误 - https://jsoneditoronline.org/?id=73911ec678c850f9b1ff131ac7ee738c
答案 0 :(得分:0)
根据你的json返回一个基于索引的数组,这就是你的代码应该如何工作。
public function getPokemonLocation($searchTerm) {
/*
Get the Pokemon via search term name
*/
$url = $this->baseUrl . $searchTerm;
$pokemonLocation = $this->getUrl($url);
/*
$pokemonLocationUrl is the encounters subset i the array URL
*/
$pokemonLocationUrl = $pokemonLocation['location_area_encounters'];
$pokemonLocationEncounters = $this->getUrl('https://pokeapi.co/' . $pokemonLocationUrl);
echo "<pre>";
print_r($pokemonLocationEncounters);
echo "</pre>";
$pokemonAreaEncounterArea = array();
$pokemonAreaEncounterGame = array();
/*
Now grab the data needed from the name of location and what game regions they are from
*/
for($i=0,$size=count($pokemonLocation);$i<$size;$i++) {
$pokemonAreaEncounterArea[] = $pokemonLocation[$i]['location_area']['name'];
$pokemonAreaEncounterGame[] = $pokemonLocation[$i]['version_details'][0]['version']['name'];
}
$pokemonLocationAndRegion = array_combine($pokemonAreaEncounterGame, $pokemonAreaEncounterArea);
// print_r($pokemonLocationAndRegion);
return $pokemonLocationAndRegion;
}
使其更清晰
for($i=0,$size=count($pokemonLocation);$i<$size;$i++) {
$pokemonAreaEncounterArea[] = $pokemonLocation[$i]['location_area']['name'];
$pokemonAreaEncounterGame[] = $pokemonLocation[$i]['version_details'][0]['version']['name'];
}
这将允许您遍历结果数组中的所有元素,并将它们放在$pokemonAreaEncounterArea
和$pokemonAreaEncounterGame
答案 1 :(得分:0)
您可以在迭代时组合目标数据:
$array=json_decode($json,true);
foreach($array as $item){
$result[$item['version_details'][0]['version']['name']]=$item['location_area']['name'];
}
var_export($result);
输出:
array (
'diamond' => 'great-marsh-area-6',
'platinum' => 'sinnoh-route-215-area',
'heartgold' => 'mt-mortar-1f',
'ruby' => 'hoenn-route-120-area',
'emerald' => 'hoenn-safari-zone-expansion-south',
'leafgreen' => 'four-island-area',
'black-2' => 'route-22-area',
'x' => 'kalos-route-3-area',
)