trying to decode json file to variable but its only returning null

时间:2017-04-09 23:23:39

标签: php json decode

I'm trying to decode a json file to a PHP variable, but the PHP variable is just null. This is my PHP:

$champions['names'] = json_decode(file_get_contents("file://D:/Xampp/htdocs/lol-champions.json"),true);
echo $champions['names']['champions'][1]['name'];

This is my json file:

"champions":[
{
  "id" : 103,
  "name" : "Ahri",
},
{ 
  "id" : 84,
  "name" : "Akali",
}]

1 个答案:

答案 0 :(得分:2)

As @Paul Crovella stated, here is the correct version :

{
"champions": [{
    "id": 103,
    "name": "Ahri"
}, {
    "id": 84,
    "name": "Akali"
}]
}

Also if you want to call it;

echo $champions->champions[1]->name;