2017-10-21 120 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