如何将变量调用到这个json响应中?

时间:2018-05-28 17:03:24

标签: php arrays json api

我尝试使用steam api并且我调用了一个响应并通过代码格式化它:

$url = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$key.'&steamids='.$id;

$json = json_decode(file_get_contents($url));
$steam_array = (array) $json;

$ json的print_r:

stdClass Object ( [response] => stdClass Object ( [players] => Array ( [0] => stdClass Object ( [steamid] => 76561198092269637 [communityvisibilitystate] => 3 [profilestate] => 1 [personaname] => NoAd [lastlogoff] => 1527501715 [profileurl] => https://steamcommunity.com/id/noad/ [avatar] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/29/29f37f0a8b3b1d3dd0d7bb4c19f6bded0a417cad.jpg [avatarmedium] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/29/29f37f0a8b3b1d3dd0d7bb4c19f6bded0a417cad_medium.jpg [avatarfull] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/29/29f37f0a8b3b1d3dd0d7bb4c19f6bded0a417cad_full.jpg [personastate] => 1 [realname] => Adam Nowak [primaryclanid] => 103582791429521408 [timecreated] => 1369457346 [personastateflags] => 0 [loccountrycode] => PL [locstatecode] => 35 [loccityid] => 35736 ) ) ) ) 

$ steam_array的print_r

Array ( [response] => stdClass Object ( [players] => Array ( [0] => stdClass Object ( [steamid] => 76561198092269637 [communityvisibilitystate] => 3 [profilestate] => 1 [personaname] => NoAd [lastlogoff] => 1527501715 [profileurl] => https://steamcommunity.com/id/noad/ [avatar] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/29/29f37f0a8b3b1d3dd0d7bb4c19f6bded0a417cad.jpg [avatarmedium] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/29/29f37f0a8b3b1d3dd0d7bb4c19f6bded0a417cad_medium.jpg [avatarfull] => https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/29/29f37f0a8b3b1d3dd0d7bb4c19f6bded0a417cad_full.jpg [personastate] => 1 [realname] => Adam Nowak [primaryclanid] => 103582791429521408 [timecreated] => 1369457346 [personastateflags] => 0 [loccountrycode] => PL [locstatecode] => 35 [loccityid] => 35736 ) ) ) ) 

如何在这个混乱中调用变量?如果我使用var_dump($ json)它会将类留给。

我试过了:

echo $steam_array->value["players"][0]['avatarfull'];   
echo $steam_array->value["avatarfull"];

我还不熟悉php中的类和对象:/

我想要像这样

$steam_array[0]['avatarfull'];

[0]很重要因为这个api请求可以捕获多个人

1 个答案:

答案 0 :(得分:0)

$steam_array是一个数组,你将它作为对象调用,因此请像下面的数组一样调用它。

$steam_array['response']->players[0]->steamid

steamid和你的所有领域。