首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从这个API中获取数据

如何从这个API中获取数据
EN

Stack Overflow用户
提问于 2014-06-09 21:08:40
回答 1查看 359关注 0票数 1

我想从API获取数据,但我有问题:

代码语言:javascript
复制
foreach ($userdata as $Summoner)
{


    $response = Unirest::get("https://community-league-of-legends.p.mashape.com/api/v1.0/$region/summoner/retrieveInProgressSpectatorGameInfo/" .
        $Summoner['NICK'], array("X-Mashape-Authorization" =>
            "$key"), null);
    $array = ($response->raw_body);
    $new = json_decode($array, true);
}

var_dump ($new);

代码语言:javascript
复制
array(5) { ["playerCredentials"]=> array(11) { ["observerEncryptionKey"]=> string(32) "nZvmi1uhrXGW4hn1bD9u9eadte7x9dGK" ["dataVersion"]=> int(0) ["playerId"]=> int(28980822) ["serverPort"]=> int(0) ["observer"]=> bool(true) ["summonerId"]=> int(0) ["championId"]=> int(0) ["observerServerIp"]=> string(12) "95.172.65.26" ["gameId"]=> int(859406155) ["observerServerPort"]=> int(8088) ["lastSelectedSkinIndex"]=> int(0) } ["dataVersion"]=> int(0) ["gameName"]=> string(15) "match-859406155" ["reconnectDelay"]=> int(0) ["game"]=> array(33) { ["practiceGameRewardsDisabledReasons"]=> array(1) { ["array"]=> array(0) { } } ["glmSecurePort"]=> int(0) ["queuePosition"]=> int(0) ["playerChampionSelections"]=> array(1) { ["array"]=> array(10) .............

但是当我循环$new

代码语言:javascript
复制
foreach ($new as $usrGmID) { 

}

我明白这一点: var_dump ($usrGmID);

代码语言:javascript
复制
array(33) { ["practiceGameRewardsDisabledReasons"]=> array(1) { ["array"]=> array(0) { } } ["glmSecurePort"]=> int(0) ["queuePosition"]=> int(0) ["playerChampionSelections"]=> array(1) { ["array"]=> array(10) { [0]=> array(6) { ["spell1Id"]=> int(14) ["spell2Id"]=> int(4) ["championId"]=> int(238) ["summonerInternalName"]=> string(4) "nali" ["selectedSkinIndex"]=> int(0) ["dataVersion"]=> int(0) } [1]=> array(6) { ["spell1Id"]=> int(4) ["spell2Id"]=> int(3) ["championId"]=> int(99) ["summonerInternalName"]=> string(9) "gamepl121".......... 

所有我不想得到的数据都消失了。

那么,我的问题是如何获得"playerId“和"gameId”,并将其推入新数组?

我试着用这种方式:

代码语言:javascript
复制
$i = 0;
foreach ($userdata as $Summoner)
{


    $response = Unirest::get("https://community-league-of-legends.p.mashape.com/api/v1.0/$region/summoner/retrieveInProgressSpectatorGameInfo/" .
        $Summoner['NICK'], array("X-Mashape-Authorization" =>
            "$key"), null);
    $array = ($response->raw_body);
    $new[$i] = json_decode($array, true);
    $i = $i + 1;
}

foreach ($new as $usrGmID)
{

array_push($sumgameid, array("playerId" => $usrGmID["playerCredentials"]["playerId"],
        "gameId" => $usrGmID["playerCredentials"]["gameId"]));

}

但这毁了我下面所有的代码行。

还有别的办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-09 21:16:50

看起来您访问数组的方式不正确。

尝试:

代码语言:javascript
复制
 $new = array(
                0 => array(
                    'playerCredentials' => array(
                        'observerEncryptionKey' => '',
                        'dataVersion' => '',
                        'playerId' => 'qwe',
                        'serverPort' => '',
                        'observer' => '',
                        'gameId' => '3425'
                    )
                )
            );

        $newArray = array();

        foreach ($new as $userGmID)
        {
            $newArray[] = array('
                playerId' => $userGmID['playerCredentials']['playerId'],
                'gameId' => $userGmID['playerCredentials']['gameId']
            );
        }

        echo '<pre>', print_r($newArray, true), '</pre>';

this outputs the values of the 2 keys you are looking for. Since you have a root array that have 0 based indices, this will look and access the child keys with your data.

最新情况:

在你原来做json调用的地方

代码语言:javascript
复制
foreach ($userdata as $Summoner)
            {
                $response = Unirest::get("https://community-league-of-legends.p.mashape.com/api/v1.0/$region/summoner/retrieveInProgressSpectatorGameInfo/" .
                    $Summoner['NICK'], array("X-Mashape-Authorization" =>
                        "$key"), null);
                $array = ($response->raw_body);
                $new[] = json_decode($array, true);
            }

通过使用[],您已经将一个新的数组索引“推入”到数组中。不需要指定索引值。我想也许你的数组是用时髦的数组索引构建的。

同时,为了避免在哪里看到的错误,请执行以下操作

代码语言:javascript
复制
foreach ($new as $userGmID)
            {
                if (isset($userGmID['playerCredentials']))
                {
                    $newArray[] = array('
                    playerId' => $userGmID['playerCredentials']['playerId'],
                        'gameId' => $userGmID['playerCredentials']['gameId']
                    );
                }
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24129093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档