简体   繁体   中英

How to get data from multi dimensional array

( 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.

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.

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']

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM