I am trying to parse the incoming JSON from the Steam Web API in order to display specific statistics from a game. In this case, the game is Rust.
JSON returned from the API:
{
"playerstats": {
"steamID":"76561198288875739",
"gameName":"",
"stats":[
{"name":"deaths","value":268},
{"name":"bullet_fired","value":11370},
{"name":"arrow_fired","value":856},
{"name":"item_drop","value":3850},
{"name":"blueprint_studied","value":293},
{"name":"death_suicide","value":103},
{"name":"death_fall","value":1},
{"name":"death_selfinflicted","value":8},
{"name":"kill_player","value":296},
{"name":"bullet_hit_player","value":1171},
{"name":"arrow_hit_entity","value":33},
{"name":"harvest.stones","value":106299},
{"name":"bullet_hit_entity","value":2484},
{"name":"harvest.cloth","value":4426},
{"name":"harvest.wood","value":379165},
{"name":"arrow_hit_building","value":90},
{"name":"kill_bear","value":34},
{"name":"kill_boar","value":46},
{"name":"kill_stag","value":31},
{"name":"kill_chicken","value":8},
{"name":"kill_horse","value":37},
{"name":"kill_wolf","value":26},
{"name":"headshot","value":364},
{"name":"arrow_hit_boar","value":15},
{"name":"arrow_hit_bear","value":68},
{"name":"arrow_hit_wolf","value":18},
{"name":"arrow_hit_stag","value":4},
{"name":"arrow_hit_chicken","value":1},
{"name":"bullet_hit_building","value":2455},
{"name":"arrow_hit_horse","value":25},
{"name":"arrow_hit_player","value":156},
{"name":"death_entity","value":18},
{"name":"death_wolf","value":1},
{"name":"death_bear","value":7},
{"name":"shotgun_fired","value":533},
{"name":"shotgun_hit_building","value":30},
{"name":"shotgun_hit_player","value":61},
{"name":"shotgun_hit_horse","value":12},
{"name":"bullet_hit_bear","value":168},
{"name":"shotgun_hit_entity","value":95},
{"name":"bullet_hit_horse","value":70},
{"name":"bullet_hit_stag","value":20},
{"name":"bullet_hit_wolf","value":31},
{"name":"bullet_hit_boar","value":14},
{"name":"bullet_hit_sign","value":9},
{"name":"wounded","value":137},
{"name":"wounded_assisted","value":53},
{"name":"wounded_healed","value":27},
{"name":"bullet_hit_playercorpse","value":96},
{"name":"bullet_hit_corpse","value":87},
{"name":"INVENTORY_OPENED","value":5292},
{"name":"CRAFTING_OPENED","value":72},
{"name":"MAP_OPENED","value":483},
{"name":"CUPBOARD_OPENED","value":78},
{"name":"ITEM_EXAMINED","value":6},
{"name":"comfort_duration","value":7701.56640625},
{"name":"calories_consumed","value":15861.1572265625},
{"name":"water_consumed","value":4199.1533203125},
{"name":"radiation_exposure_duration","value":799.53271484375},
{"name":"cold_exposure_duration","value":8946.0244140625},
{"name":"hot_exposure_duration","value":2202.734619140625},
{"name":"melee_strikes","value":1940},
{"name":"melee_thrown","value":47},
{"name":"placed_blocks","value":164},
{"name":"upgraded_blocks","value":1050},
{"name":"arrows_shot","value":250},
{"name":"seconds_speaking","value":79.7517318725585938},
{"name":"acquired_lowgradefuel","value":748},
{"name":"acquired_metal.ore","value":24883},
{"name":"acquired_scrap","value":494},
{"name":"topology_road_duration","value":785},
{"name":"destroyed_barrels","value":113}
],
"achievements":[
{"name":"CONSTRUCT_BASE","achieved":1},
{"name":"UPGRADE_BASE","achieved":1},
{"name":"COLLECT_50_LGF","achieved":1},
{"name":"COLLECT_300_METAL_ORE","achieved":1},
{"name":"VISIT_ROAD","achieved":1},
{"name":"COLLECT_65_SCRAP","achieved":1},
{"name":"DESTROY_10_BARRELS","achieved":1}
]
}
}
I am having trouble using this data to display the desired stat. For example, I want to display total player kills ("name":"kill_player","value":296
)
How would I manage to do this? I have parsed some simple JSON before but nothing this complex.
Appreciate the help!
EDIT
I have managed to get all of the names with all of the values by doing this:
$rust_info_result = json_decode($rust_info_result);
$rust_info_result = $rust_info_result->playerstats->stats;
foreach($rust_info_result as $r2)
{
echo $r2->name; echo " "; echo $r2->value; echo "<br>";
}
But I still can't find a way to get details for a specific stat. Such as kill_player.
EDIT #2
I manged to get this working. For anyone else who may have the same issue as me I found the following method worked perfectly for me:
$rust_info_result = json_decode($rust_info_result);
$rust_info_result = $rust_info_result->playerstats->stats;
$stats = [];
foreach ($rust_info_result as $r2)
{
$stats[$r2->name] = $r2->value;
}
echo "Player Kills: " . $stats['kill_player'];