Mar*_*aro 1 php foreach json decode
我如何从存在“ gameid”且值为“ 730”的玩家那里获取所有“ steamid”?它显示:
NOTICE未定义属性:第31行的stdClass :: $ gameid,因为在每个元素中都不存在“ gameid”
码:
$players = json_decode(file_get_contents("test.json"));
foreach($players->response->Players as $mydata)
{
if($players->gameid == "730"){
echo $mydata->steamid . "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
我的json:
{
"response": {
"players": [
{
"steamid": "76561198273176399",
"loccountrycode": "US"
},
{
"steamid": "76561198386141115",
"gameid": "730",
"loccountrycode": "DE"
},
{
"steamid": "76561198019025166",
"gameid": "730",
"loccountrycode": "RU"
},
{
"steamid": "76561198010529217",
"loccountrycode": "DK"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢。最诚挚的问候。
更改
$players->response->Players
if($players->gameid == "730"){
Run Code Online (Sandbox Code Playgroud)
至
$players->response->players // small letter "p"
if($mydata->gameid == "730"){
Run Code Online (Sandbox Code Playgroud)
和...一样 $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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |