1

how can i get all "steamids" from Players where "gameid" exist and have the value "730"? it Shows:
NOTICE Undefined property: stdClass::$gameid on line number 31 because "gameid" doesnt exist in every element

Code:

$players = json_decode(file_get_contents("test.json"));
foreach($players->response->Players as $mydata)
{
if($players->gameid == "730"){
echo $mydata->steamid . "\n";
}

}   

My json:

{
    "response": {
        "players": [
            {
                "steamid": "76561198273176399",
                "loccountrycode": "US"
            },
            {
                "steamid": "76561198386141115",
                "gameid": "730",
                "loccountrycode": "DE"
            },
            {
                "steamid": "76561198019025166",
                "gameid": "730",
                "loccountrycode": "RU"
            },
            {
                "steamid": "76561198010529217",
                "loccountrycode": "DK"
            }
        ]

    }
}

Thanks a lot. With best regards.

Maro
  • 55
  • 1
  • 7
  • start `players` with small letter in foreach and `if($mydata->gameid` – splash58 Oct 21 '17 at 16:03
  • @splash58   thanks, but it Shows me: NOTICE Undefined property: stdClass::$gameid on line number 31 because "gameid" doesnt exist in every element – Maro Oct 21 '17 at 16:11

1 Answers1

5

Change

$players->response->Players
if($players->gameid == "730"){

To

$players->response->players // small letter "p"
if($mydata->gameid == "730"){

It's the same as $mydata->steamid

Since not every player has a gameid it's better to check on that aswell:

if( isset($mydata->gameid) && $mydata->gameid === '730' ) // or check on empty if the "gameid" can be set but also can be empty
SuperDJ
  • 7,488
  • 11
  • 40
  • 74