0

( Full pastebin: https://pastebin.com/C4qV5YYv)

I'm trying to select data from a (long) multi dimensional array, but with all things I tried the page just showed as blank. I'm trying to access the data with the name instead of it's rank in the list.

{
    "playerstats": {
        "steamID": "76561198035223060",
        "gameName": "ValveTestApp260",
        "stats": [
            {
                "name": "total_kills",
                "value": 38694
            },
            {
                "name": "total_deaths",
                "value": 33362
            },
            {
                "name": "total_time_played",
                "value": 2148546
            },
            {
                "name": "total_planted_bombs",
                "value": 770
            },
            {
                "name": "total_defused_bombs",
                "value": 271
            },
            {
                "name": "total_wins",
                "value": 12394
            }, So on and so on......

I'm currently using this to get data from the array: $kills = $jsonforgame['playerstats']['stats'][0]['value'];

This works when you only need a couple of values, but it gets really tidy when I need to select values further down, like; $hit = $jsonforgame['playerstats']['stats'][47]['value'];

Is there anyway for me to select the stats with the name, like this: $hit = $jsonforgame['playerstats']['stats']['total_shots_fired']['value']; instead of the number.

Thanks in advance.

6
  • you have to convert to an array with json_decode after that uou can fetch via key name Commented May 18, 2017 at 9:03
  • I already do that here: $jsonforgame = json_decode($SteamStatsForGameResult, true); Commented May 18, 2017 at 9:04
  • so what is the output ? Commented May 18, 2017 at 9:05
  • How are you getting $SteamStatsForGameResult? Can you share the full code so we can see what you're attempting please? Commented May 18, 2017 at 9:06
  • Why you not use foreach Commented May 18, 2017 at 9:06

3 Answers 3

1

You may go for something like this:

function getStat($data, $name)
{
    return array_filter($data, function($item) use ($name) {
        return $item && $item['name'] === $name;
    })[0]['value'];
}

$hit = getStat($jsonforgame['playerstats']['stats'], 'total_shots_fired');

But the more efficient way would be to change your stats api so it serves key-value pairs, if it is possible, ofc.

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

5 Comments

When using this my page just show's up blank without an error message of some sort
made a small edit, forgot you're converting to array, not stdClass
Now I get an error printed out: Notice: Trying to get property of non-object in D:\XAMPP\htdocs\site\csgo\stats.php on line 64
Edited again to facilitate a possible problem, but i'm afraid i cannot do much more without seeing data and code.
Edited again, to array
0

One way would be to change how you create the array as so:

{
    "playerstats": {
        "steamID": "76561198035223060",
        "gameName": "ValveTestApp260",
        "stats": 
            {
                "total_kills": 38694,
                "total_deaths": 33362,
                "total_time_played": 2148546,
                ...
            }

then simply access by $kills = $jsonforgame['playerstats']['stats']['total_kills']

1 Comment

I'm getting the data with cURL, so it's not possible to change the array structure
0

In case you can't change the array, you could also try

$specs = array("total_kills", "total_deaths", "total_time_played"); // and so on

foreach ( $specs as $spec )
  $$spec = $jsonforgame['playerstats']['stats'][ $spec ]['value'];

After that you can use each spec by name, for example $total_kills

If you want to use shorter variable names you can change the code like this

$specs = array(
      "kills"  => "total_kills",
      "died"   => "total_deaths", 
      "played" => "total_time_played"
 ); // and so on

foreach ( $specs as $key => $spec )
  $$key = $jsonforgame['playerstats']['stats'][ $spec ]['value'];

echo $kills; // output $jsonforgame['playerstats']['stats']['total_kills']['value']

Another approach

$count = count($jsonforgame['playerstats']['stats']);

for ( $i = 0; $i < $count; $i++ ) {
    $name  = $jsonforgame['playerstats']['stats'][ $i ]['name'];
    $value = $jsonforgame['playerstats']['stats'][ $i ]['value'];
    $$name = $value;
}

and with use of the array with shorter variable names

$specs = array(
   "total_kills"       => "kills",
   "total_deaths"      => "died", 
   "total_time_played" => "played",
 ); // and so on

$count = count($jsonforgame['playerstats']['stats']);

for ( $i = 0; $i < $count; $i++ ) {
    $name  = $specs[ $jsonforgame['playerstats']['stats'][ $i ]['name'] ];
    $value = $jsonforgame['playerstats']['stats'][ $i ]['value'];
    $$name = $value;
}

5 Comments

When I tried this It gave me a blank page without errors :(
Must be something different about your data then.
I have added an example. If your $jsonforgame array is structured differently then you will have to adjust it.
added another approach
and anther one with use of the shorter variable names