Meler Lawler
Meler Lawler

Reputation: 175

How do I get variable values from this JSON array?

So I have a URL that returns the following:

[{"_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}]

I have learned how to get the value of a key in an array when the URL returns something simpler. For example if the URL returns:

{"id":34743514,"accountId":49161997,"name":"League of Fiddle","profileIconId":786,"revisionDate":1514093712000,"summonerLevel":52}

I can echo the id with this code:

$json = file_get_contents(URL);
$data = json_decode($json, true);
echo $data['id'];

That's easy. But when I try to use the same code for the more complicated stuff, like say I want to get the value of _id champ2_id, I've tried:

$json = file_get_contents(URL);
$data = json_decode($json, true);
echo $data['_id']['champ2_id'];

But this says _id is an undefined index. What am I doing wrong?

Upvotes: 2

Views: 43

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133380

should be

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

Upvotes: 3

Related Questions