1
$stat_url = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=ECFB8FBC534B61C454899F4D7E99BB71&steamid=76561198129638121';
$stat_json = file_get_contents($stat_url);
$stat_decoded = json_decode($stat_json, true);

I have only been able to target the value by number, but the api doesnt always return it in the same order. So I want to be able to target the name (EX: total_kills) and then get the value (85990).

print_r($stat_decoded['playerstats']['stats']);



Array (
   [
      0
   ]   => Array (   [
      name
   ]   => total_kills   [
      value
   ]   => 85990 )   [
      1
   ]   => Array (   [
      name
   ]   => total_deaths   [
      value
   ]   => 88675 )   [
      2
   ]   => Array (   [
      name
   ]   => total_time_played   [
      value
   ]   => 7051848 )   [
      3
   ]   => Array (   [
      name
   ]   => total_planted_bombs   [
      value
   ]   => 2131 )   [
      4
   ]   => Array (   [
      name
   ]   => total_defused_bombs   [
      value
   ]   => 1234 )   [
      5
   ]   => Array (   [
      name
   ]   => total_wins   [
      value
   ]   => 47204 )

Don't worry about the API key, it will be reset.

2 Answers 2

2

You can convert the array to be in a format more convenient to work with, for example, you can set the names to be the keys in the new array, like this:

$stat_url = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=ECFB8FBC534B61C454899F4D7E99BB71&steamid=76561198129638121';
$stat_json = file_get_contents($stat_url);
$stat_decoded = json_decode($stat_json, true);

$assoc_arr = array_reduce($stat_decoded, function ($result, $item) {
    $result[$item['name']] = $item['value'];
    return $result;
}, []);

From here you can get the value by approaching the value in it's key: $assoc_arr['total_kills']

Sign up to request clarification or add additional context in comments.

Comments

0

Although you have already accepted an answer, a simpler version would be to use array_column()...

$assoc_arr = array_column($stat_decoded['playerstats']['stats'], "value", "name");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.