所以我有一个返回以下内容的 URL:
[{"_id":{"champ2_id":63,"champ1_id":2,"role":"TOP"},"count":4,"champ1":{"thirtyToEnd":0,"goldEarned":10727.5,"zeroToTen":0,"minionsKilled":158,"winrate":0,"assists":6.25,"role":"TOP","deaths":6,"kills":4,"wins":0,"totalDamageDealtToChampions":17350.75,"twentyToThirty":0,"tenToTwenty":0,"neutralMinionsKilledTeamJungle":1.75,"killingSprees":0.75,"weighedScore":27214.5375},"champ2":{"twentyToThirty":0,"wins":4,"winrate":1,"kills":5.75,"neutralMinionsKilledTeamJungle":5,"totalDamageDealtToChampions":21881.25,"role":"TOP","assists":7,"tenToTwenty":0,"thirtyToEnd":0,"zeroToTen":0,"goldEarned":12371.75,"killingSprees":1.25,"minionsKilled":140.5,"deaths":4.25,"weighedScore":33166.587499999994}]
我已经学会了当 URL 返回更简单的内容时如何获取数组中键的值。例如,如果 URL 返回:
{"id":34743514,"accountId":49161997,"name":"League of Fiddle","profileIconId":786,"revisionDate":1514093712000,"summonerLevel":52}
我可以使用以下代码回显 id:
$json = file_get_contents(URL);
$data = json_decode($json, true);
echo $data['id'];
这很容易。但是当我尝试使用相同的代码来处理更复杂的事情时,比如我想获取 _id champ2_id 的值,我尝试过:
$json = file_get_contents(URL);
$data = json_decode($json, true);
echo $data['_id']['champ2_id'];
但这表示 _id 是一个 undefined index 。我做错了什么?
最佳答案
应该是
$data[0]['_id']['champ2_id'];
关于php - 如何从此 JSON 数组获取变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47963790/