2016-05-04 18 views
0

私はHalo 5 APIのリーダーボードを持っています。リーダーボードごとに、今シーズンのプレイリストのトップ100ユーザーがいます。それはシーズン-IDで要求した要素のJSON配列から最後の配列インデックスを取得します - laravel 5.2

// $baseURL = 'https://www.haloapi.com/stats/{title}/player-leaderboards/csr/{seasonId}/{playlistId}[?count]'; 

$baseURL = 'https://www.haloapi.com/stats/h5/player-leaderboards/csr/8787875e-d2c6-4c50-b949-38e22728f9f4/c98949ae-60a8-43dc-85d7-0feb0b92e719?count=100'; 

ワン:私がいる問題は、私はこのデータを取得するための呼び出しを行う必要があり、これはそれが要求するものです。新しいシーズンが始まるため、今月のIDは毎月変わります。ですから毎月これを更新しなければなりません。効率的ではありません。

シーズンの配列をループし、そのループの最後の配列インデックスを取得して、そのシーズンのIDを取得する必要があります。それから私は毎回更新する必要はありません、それは自動的にそれを行います。

public function getSeason() { 

     $client = new GuzzleHttp\Client(); 

     $baseURL = 'https://www.haloapi.com/metadata/h5/metadata/seasons'; 

     $res = $client->request('GET', $baseURL, [ 
      'headers' => [ 
       'Ocp-Apim-Subscription-Key' => 'MY KEY' 
      ] 
     ]); 

     if ($res->getStatusCode() == 200) { 
      return $result = json_decode($res->getBody()); 
     } elseif ($res->getStatusCode() == 404) { 
      return $result = redirect()->route('/'); 
     } 

     return $res; 
    } 

これは私が今それをDD方法である:ここで

は、私はすべての季節を取得するために作るの呼び出しです

public function getSeasonsArray($Seasons) { 
     $x = $Seasons; 
     dd($x); 
    } 

そして、これが結果です:

array:6 [▼ 
    0 => {#204 ▶} 
    1 => {#209 ▶} 
    2 => {#216 ▶} 
    3 => {#222 ▶} 
    4 => {#228 ▶} 
    5 => {#234 ▼ 
    +"playlists": array:5 [▶] 
    +"iconUrl": "https://content.halocdn.com/media/Default/forums/badges/thumbs/badge-enlisted-45x45-0d172751d2aa4d4691cf966c111b9ece.png" 
    +"name": "May 2016 Season" 
    +"startDate": "2016-05-02T17:00:00Z" 
    +"endDate": null 
    +"isActive": true 
    +"id": "8787875e-d2c6-4c50-b949-38e22728f9f4" 
    +"contentId": "8787875e-d2c6-4c50-b949-38e22728f9f4" 
    } 
] 

この配列をループしてこの配列の最後のインデックスを取得して、シーズンのIDを取得するにはどうすればよいですか?


これは私が私の呼び出しは、このシーズンのために特定のプレイリストを取得するために作る方法です:

public function getTeamArenaLeaderboards() { 


     $Seasons = $this->getSeason(); 
     $SeasonsArray = $this->getSeasonsArray($Seasons); 


     $client = new GuzzleHttp\Client(); 

     // $baseURL = 'https://www.haloapi.com/stats/{title}/player-leaderboards/csr/{seasonId}/{playlistId}[?count]'; 

     $baseURL = 'https://www.haloapi.com/stats/h5/player-leaderboards/csr/8787875e-d2c6-4c50-b949-38e22728f9f4/c98949ae-60a8-43dc-85d7-0feb0b92e719?count=100'; 

     $res = $client->request('GET', $baseURL, [ 
      'headers' => [ 
       'Ocp-Apim-Subscription-Key' => 'MY KEY' 
      ] 
     ]); 

     if ($res->getStatusCode() == 200) { 
      return $result = json_decode($res->getBody()); 
     } elseif ($res->getStatusCode() == 404) { 
      return $result = redirect()->route('/'); 
     } 

     return $res; 
    } 

/************* EDIT *****それは、これを行うことにより、作業ガット

***************:

public function getTeamArenaLeaderboards() { 

     // Get all the Seasons 
     $Seasons = $this->getSeason(); 

     // Get the last Season of the array 
     $lastSeason = end($Seasons); 

     // Get the ID of the last Season 
     $last = $lastSeason->id; 


     $client = new GuzzleHttp\Client(); 


$baseURL = 'https://www.haloapi.com/stats/h5/player-leaderboards/csr/' . $last .'/c98949ae-60a8-43dc-85d7-0feb0b92e719?count=100'; 
+2

う[ 'array_pop'](http://php.net/manual/en/function.array-pop.php)の作品? –

+2

私はあなたが望むものを得るために 'key(end($ arr))'を使うことができると思います。それがあなたが望む最後のメンバーのインデックスでない限り、私は誤解しました。 – Ohgodwhy

答えて

1

使用end()

echo end($Seasons)['id']; 

またはそれ以前のPHPのバージョンについて:あなたのための

$last = end($Seasons); 
echo $last['id']; 
+0

アレイの最後のIDを取得しました。これをヘッダーに挿入する必要があります。ちょっと待って。 – David

+0

はい、ありがとうございます。私はそれを働かせました。 – David

関連する問題