2014-06-09 6 views
1

이 API에서 데이터를 얻을 수 있지만이 문제가 방법 :내가 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 (새 $)를;

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

그러나 때 새로운 루프 $

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

} 

을하지만이 아래의 모든 내 코드 라인을 파괴.

다른 방법이 있습니까?

+0

당신은 아마이 내 키없는 특정 값 귀하의 배열 ('observerEncryptionKey') ... – didierc

+0

를 공유하고 싶지 않은 유일한 예 : P – user3705982

답변

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

을 원래의 foreach에

이미 배열. 인덱스 값을 지정할 필요가 없습니다. 아마 배열이 펑키 배열 색인으로 빌드되고있는 것 같아요.

또한 당신이보고 오류를 방지하기 위해이

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

흠, 배열과 같아서 foreach에 먹이를주고 0 인덱스를 넘지 않습니다. – Eli

+0

그래서 코드를 다시 작성해야합니다. Thx 당신의 도움. – user3705982

관련 문제