如何解码此json中的“值”?
{
"playerstats": {
"steamID": "76561198054387800",
"gameName": "ValveTestApp260",
"stats": [
{
"name": "total_kills",
"value": 19229
},
我已经尝试过了:
$str = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=".$steamauth['apikey']."&steamids=".$_SESSION['steamid']);
$json = json_decode($str, true);
$totalkills = $json['playerstats']['stats'][0]['total_kills']['value'];
echo $totalkills . PHP_EOL;
请帮帮我:/
答案 0 :(得分:0)
我想这会奏效。
$totalkills = $json['playerstats']['stats'][0]['value'];
echo $totalkills . PHP_EOL;
但由于统计数据是一个数组,因此假设总数将始终位于数组的第0位,这有点不安全。循环遍历数组并找到您正在寻找的值更安全。
foreach ($json['playerstats']['stats'] as $i) {
if ($i['name'] == "total_kills") {
echo $i['name'] . " :" . $i['value'];
}
}