我一直在寻找interwebz的简单答案,但找不到任何答案.所以,问题是:
我要解码一些JSON以查看是否存在值; 不过,我认为我做得不对.我想检查appid:730的值是否存在.
这是JSON:
{
response: {
game_count: 106,
games: [
{
appid: 10,
playtime_forever: 67
},
{
appid: 730,
playtime_forever: 0
},
{
appid: 368900,
playtime_forever: 0
},
{
appid: 370190,
playtime_forever: 0
},
]
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我要的:
$json = file_get_contents('JSON URL HERE');
$msgArray = json_decode($json, true);
if (appid: 730 exists) {
...
}
Run Code Online (Sandbox Code Playgroud)
谢谢,希望我解释得足够多.
首先,您的 json 无效。请参阅下面字符串减速中的注释(这可能是您问题中的错字)。
$json = '{
"response": {
"game_count": 106,
"games": [
{
"appid": 10,
"playtime_forever": 67
},
{
"appid": 730,
"playtime_forever": 0
},
{
"appid": 368900,
"playtime_forever": 0
},
{
"appid": 370190,
"playtime_forever": 0
} // <------ note the lack of `,`
]
}
}';
$arr = json_decode($json, true);
foreach($arr['response']['games'] as $game) {
if($game['appid'] === 730) { // I would strictly check (type) incase of 0
echo "exists"; // or do something else
break; // break out if you dont care about the rest
}
}
Run Code Online (Sandbox Code Playgroud)
我们只是遍历游戏数组并检查它的appid. 然后我们只是做一些事情,然后打破循环以防止开销。
| 归档时间: |
|
| 查看次数: |
1124 次 |
| 最近记录: |