2

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?

2
  • it's an array inside an array. Commented Dec 24, 2017 at 20:27
  • Or a multidimensional array. Try $data[0]['_id']['champ2_id'] Commented Dec 24, 2017 at 20:29

1 Answer 1

3

should be

$data[0]['_id']['champ2_id'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Going to select this as best answer when it lets me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.