如何从多维数组中获取数据

时间:2017-05-18 09:01:08

标签: php arrays

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

我正在尝试从(长)多维数组中选择数据,但是我尝试过的所有内容都显示为空白。我正在尝试使用名称访问数据而不是列表中的排名。

{
    "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......

我目前正在使用它从数组中获取数据:$kills = $jsonforgame['playerstats']['stats'][0]['value'];

当你只需要几个值时,这是有效的,但当我需要选择更低的值时,它会非常整洁,比如; $hit = $jsonforgame['playerstats']['stats'][47]['value'];

我是否有选择名称的统计数据,如下所示:$hit = $jsonforgame['playerstats']['stats']['total_shots_fired']['value'];而不是数字。

提前致谢。

3 个答案:

答案 0 :(得分:1)

你可能会这样:

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');

但更有效的方法是更改​​你的统计数据api,以便它可以提供键值对,如果可能的话,那就是.c。

答案 1 :(得分:0)

一种方法是改变你创建数组的方式:

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

然后只需通过$ kills = $ jsonforgame ['playerstats'] ['stats'] ['total_kills']

进入

答案 2 :(得分:0)

如果您无法更改阵列,也可以尝试

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

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

之后,您可以按名称使用每个规范,例如$total_kills

如果您想使用较短的变量名称,可以像这样更改代码

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

另一种方法

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

并使用具有较短变量名的数组

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