I got this big problem,and I can’t find a solution..
I got this Json being returned to me from an API,but I don’t really know how can I get the name of the hero by using his id ?
[
{ "id":0, "name":"N/A" },
{ "id":1, "name":"John" },
{ "id":2, "name":"Doom" }
]
Tried something like :
$data = json_decode($json,true);
$id = $data[0]['id'];
$name = $data[0][$id]['name'];
But I can’t really make this work…
Thank you in advance!
in your example you are looking to get index ‘0’ in the JSON array, index 0 would be:
This index 0 is now a key-value pair so you can simply call
If you wanted to do it for all of the elements in the array then you could use a for-loop or a foreach-loop.
To get the name for the ID you would have to do something like the following:
See http://pastebin.com/Rtq3NbQd for full code example