2017-10-21 123 views
1

我怎樣才能得到所有「steamids」來自哪裏「遊戲ID」存在並且具有值「730」的玩家? 它表明:
注意未定義的屬性:stdClass的:: $遊戲ID上線人數31 因爲 「遊戲ID」 犯規存在於每一個元素PHP的Json解碼對象存在

代碼:

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

} 

我的JSON:

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

    } 
} 

非常感謝。最好的祝福。

+0

在foreach和'if mydata-> gameid'開頭'''小號字母'''''''mydata-> gameid' – splash58

+0

@ splash58 謝謝,但它顯示我:NOTICE未定義的屬性:stdClass :: $ gameid在第31行,因爲「gameid」存在於每個元素 – Maro

回答

4

變化

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

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

這是一樣$mydata->steamid

因爲不是每個球員都有gameid最好還是檢查上藏漢:

if(isset($mydata->gameid) && $mydata->gameid === '730') // or check on empty if the "gameid" can be set but also can be empty 
+0

謝謝,但它顯示我:注意未定義的屬性:行號31上的stdClass :: $ gameid,因爲「gameid」不存在於每個元素 – Maro

+0

已更新答案 – SuperDJ