【问题标题】:How to call variables into this json response?如何将变量调用到这个 json 响应中?
【发布时间】:2018-05-28 17:03:23
【问题描述】:

我尝试使用 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 请求可以捕获多个人

【问题讨论】:

    标签: php arrays json api


    【解决方案1】:

    您好$steam_array 是一个数组,您将其称为对象,因此请像数组一样将其称为数组,请在下方查看。

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

    steamid 还有你的所有领域。

    【讨论】:

    • @NoAd 欢迎 :)
    猜你喜欢
    • 1970-01-01
    • 2020-05-19
    • 2019-02-16
    • 1970-01-01
    • 2011-08-09
    • 2016-08-01
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多