Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从这个JSON数组中获取变量值?_Php_Json - Fatal编程技术网

Php 如何从这个JSON数组中获取变量值?

Php 如何从这个JSON数组中获取变量值?,php,json,Php,Json,因此,我有一个返回以下内容的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,"totalDamageDealtT

因此,我有一个返回以下内容的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'];
那很容易。但是当我尝试对更复杂的东西使用相同的代码时,比如说我想得到_idchamp2_id的值,我尝试了:

$json = file_get_contents(URL);
$data = json_decode($json, true);
echo $data['_id']['champ2_id'];
但这表明_id是一个未定义的索引。我做错了什么?

应该是

$data[0]['_id']['champ2_id'];

它是一个数组中的数组。或者是一个多维数组。试试
$data[0][''u id']['champ2\u id']
谢谢!当它允许我的时候,我会选择这个作为最佳答案