如何从此API获取数据

时间:2014-06-09 21:08:41

标签: php

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

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

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

foreach ($new as $usrGmID) { 

}

我明白了: var_dump($ usrGmID);

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”]并将其推入新阵列?

我这样试试:

$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"]));

}

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

还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

看起来你正确地访问了数组。

尝试:

 $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调用的原始foreach中

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

使用[]你是&#34;推&#34;已经有一个新的数组索引到数组中。无需指定索引值。我想也许你的数组正在用时髦的数组索引构建。

也是为了避免你看到这样做的错误

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