【发布时间】:2017-09-13 20:03:26
【问题描述】:
我有这个代码:
// Champion name and splash art
$endpointChampion = file_get_contents("https://global.api.riotgames.com/api/lol/static-data/BR/v1.2/champion/".$championMastery."?api_key=MYKEY");
$jsonChampion = json_decode($endpointChampion, true);
foreach ($jsonChampion as $champion) {
if (isset($jsonChampion['key'])) {
$championKey = $champion['key'];
}
}
但是这个 $championKey 变量返回“o”并且屏幕上会提示 3 个警告:
警告:E:\xampp\htdocs\riot\index.php 第 41 行中的非法字符串偏移 'key'
我还尝试使用isset() 验证条目,但似乎无法正常工作。
$championMastery 在此处检索:
$endpointMastery = file_get_contents("https://br.api.riotgames.com/championmastery/location/BR1/player/8083198/champions?api_key=MYKEY");
$jsonMastery = json_decode($endpointMastery, true);
foreach ($jsonMastery as $mastery) {
$championMastery = $mastery['championId'];
$masteryLevel = $mastery['championLevel'];
}
【问题讨论】:
-
你为什么要在
$jsonChampion和$champion之间切换? -
isset($jsonChampion['key'])必须是isset($champion['key']) -
打印
$jsonChampion的输出并展示给我们 -
foreach 循环完全没有必要,感谢@IgnacioVazquez-Abrams 的启发。我只是删除它并设置 $championKey = $jsonChampion['key'];