John Smith John Smith - 1 year ago 118
PHP Question

How do I target nested JSON objects in php, and echo them?

Okay, so I think you get what I wanna do by just watching the code.

//Get JSON text file (Steam API)
$json = file_get_contents('');

//Decode JSON
$game_json = json_decode($json, true);

//Target game name and echo it
echo $game_json['name'];

The JSON itself comes in this order (unstructured, very sorry):

{"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"

So my target is
""name":"Tropico 4: Steam Special Edition""
, which is what I want to echo on my page. I'm not sure if it helps, but
appears once, is something like
needed in my code to target the first? Is the nesting what's stopping me here, or is the
an incorrect way of targeting?

ANY tips and/or help will be much appreciated. Thanks.

Answer Source

In the future, use print_r($game_json) to check the array structure.

$json = file_get_contents('');
$game_json = json_decode($json, true);
echo $game_json['57690']['data']['name'];
//Tropico 4: Steam Special Edition
echo $game_json['57690']['data']['required_age'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download