0

I'm using steam API to get a specific playtime_forever for a game(appid). Here's the JSON response that I got :

response: {
    game_count: 7,
    games: [
        {
            appid: 730,
            playtime_2weeks: 300,
            playtime_forever: 204909,
            playtime_windows_forever: 48633,
            playtime_mac_forever: 0,
            playtime_linux_forever: 0
        },
        {
            appid: 291550,
            playtime_forever: 4905,
            playtime_windows_forever: 4433,
            playtime_mac_forever: 0,
            playtime_linux_forever: 0
        },
        {
            appid: 550650,
            playtime_2weeks: 3,
            playtime_forever: 12191,
            playtime_windows_forever: 1405,
            playtime_mac_forever: 0,
            playtime_linux_forever: 0
        },
        {
            appid: 573090,
            playtime_forever: 2945,
            playtime_windows_forever: 3,
            playtime_mac_forever: 0,
            playtime_linux_forever: 0
        },
        {
            appid: 601510,
            playtime_2weeks: 766,
            playtime_forever: 47983,
            playtime_windows_forever: 36461,
            playtime_mac_forever: 0,
            playtime_linux_forever: 0
        },
        {
            appid: 222880,
            playtime_forever: 284,
            playtime_windows_forever: 3,
            playtime_mac_forever: 0,
            playtime_linux_forever: 0
        },
    ]
}

I decoded the JSON and loop through it but I got the error

Notice: Trying to get property 'response' of non-object in ...

Here's my PHP code for the looping through JSON

$json = json_decode(file_get_contents($api_game_stats), true);
                        
$playtime ="";
foreach($json->response as $response){
    foreach($response->games as $games){
            if($games->appid==730){
                $playtime = $games->playtime_forever;
            }
        }
    }

How do I loop properly through that JSON response?

ardant
  • 23
  • 6
  • remove the seconde parameter of the json_decode function, to decode your json code to an php object. `$json = json_decode(file_get_contents($api_game_stats));` – GNassro Sep 03 '20 at 00:35
  • it works when i echo this `echo $json->response->games[0]->appid;` but the loop still cant get through "games" loop – ardant Sep 03 '20 at 00:42
  • just use one foreach to loop into the games.`foreach($json->response->games as $games)`, and delete the other foreach. – GNassro Sep 03 '20 at 01:05

0 Answers0