从数组中获取信息

时间:2014-07-01 14:21:33

标签: php json

我正在尝试从此数组中获取信息,但只从appid获取值为“240”的信息。

(我想知道appid中有“240”的“playtime_2weeks”)

这是我的代码

$link = file_get_contents('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=' . $key . '&steamid=' . $id . '&format=json');
$myarray = json_decode($link, true);

$info = ($myarray['response']['games'][0]['appid']);

阵列副本:

{
    "response": {
        "game_count": 44,
        "games": [
            {
                "appid": 240,
                "playtime_2weeks": 60,
                "playtime_forever": 25979
            },
            {
                "appid": 300,
                "playtime_forever": 271
            },
            {
                "appid": 320,
                "playtime_forever": 0
            },
            {
                "appid": 340,
                "playtime_forever": 0
            },
            {
                "appid": 4000,
                "playtime_forever": 419
            },
            {
                "appid": 12210,
                "playtime_forever": 6743
            },
            {
                "appid": 17470,
                "playtime_forever": 0
            },
            {
                "appid": 24740,
                "playtime_forever": 0
            },
            {
                "appid": 1250,
                "playtime_forever": 377
            },
            {
                "appid": 35420,
                "playtime_forever": 0
            },
            {
                "appid": 39800,
                "playtime_forever": 0
            },
            {
                "appid": 550,
                "playtime_forever": 171
            },
            {
                "appid": 223530,
                "playtime_forever": 0
            },
            {
                "appid": 8190,
                "playtime_forever": 75
            },
            {
                "appid": 49800,
                "playtime_forever": 4
            },
            {
                "appid": 17410,
                "playtime_forever": 16
            },
            {
                "appid": 9930,
                "playtime_2weeks": 118,
                "playtime_forever": 118
            },
            {
                "appid": 22350,
                "playtime_forever": 241
            },
            {
                "appid": 400,
                "playtime_forever": 1
            },
            {
                "appid": 620,
                "playtime_forever": 124
            },
            {
                "appid": 644,
                "playtime_forever": 0
            },
            {
                "appid": 72200,
                "playtime_forever": 8
            },
            {
                "appid": 44320,
                "playtime_2weeks": 84,
                "playtime_forever": 84
            },
            {
                "appid": 46540,
                "playtime_forever": 0
            },
            {
                "appid": 205790,
                "playtime_forever": 0
            },
            {
                "appid": 108800,
                "playtime_forever": 0
            },
            {
                "appid": 730,
                "playtime_2weeks": 958,
                "playtime_forever": 5998
            },
            {
                "appid": 208500,
                "playtime_2weeks": 18,
                "playtime_forever": 1844
            },
            {
                "appid": 96300,
                "playtime_forever": 4
            },
            {
                "appid": 220240,
                "playtime_forever": 1593
            },
            {
                "appid": 44690,
                "playtime_forever": 1
            },
            {
                "appid": 226700,
                "playtime_forever": 251
            },
            {
                "appid": 43160,
                "playtime_forever": 66
            },
            {
                "appid": 231430,
                "playtime_forever": 709
            },
            {
                "appid": 244850,
                "playtime_forever": 1227
            },
            {
                "appid": 47790,
                "playtime_forever": 0
            },
            {
                "appid": 47830,
                "playtime_forever": 0
            },
            {
                "appid": 232810,
                "playtime_forever": 10
            },
            {
                "appid": 223670,
                "playtime_2weeks": 242,
                "playtime_forever": 867
            },
            {
                "appid": 252490,
                "playtime_2weeks": 5,
                "playtime_forever": 2422
            },
            {
                "appid": 254130,
                "playtime_2weeks": 493,
                "playtime_forever": 493
            },
            {
                "appid": 221100,
                "playtime_2weeks": 17,
                "playtime_forever": 1659
            },
            {
                "appid": 247730,
                "playtime_2weeks": 2,
                "playtime_forever": 2
            },
            {
                "appid": 3050,
                "playtime_forever": 52
            }
        ]

    }
}

1 个答案:

答案 0 :(得分:0)

这是一个JSON响应,而不是一个数组。 您需要先将其转换为数组,然后查找所需的值。

$response = '{
    "response": {
        "game_count": 44,
        "games": [
            {
                "appid": 240,
                "playtime_2weeks": 60,
                "playtime_forever": 25979
            },
            {
                "appid": 300,
                "playtime_forever": 271
            },
            ....
            {
                "appid": 3050,
                "playtime_forever": 52
            }
        ]

    }
}';

$responseArray = json_decode($response, true);
foreach ($responseArray['response']['games'] as $game) {
    if ($game['appid'] == '240') {
        echo "Playtime is: " . $game['playtime_2weeks'];
    }
}

OR

array_walk($responseArray['response']['games'], function($game) { 
    if ($game['appid'] == '240') {
        echo "Playtime is: " . $game['playtime_2weeks'];
    }
});