David David - 7 months ago 38
PHP Question

Get the last array index from a JSON array - laravel 5.2

I have a leader board for the Halo 5 API, and for each leader board, I have the top 100 users for a particular Playlist, for this season. The problem I'm having is I have to make a call to get this data, and this is what it requests:

// $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';


One of the elements it requests is the season-ID. Every month this season ID changes, because a new season starts. So I have to update this every month, which is not efficient.

I need to loop through the season array and get the last array index for that loop so I can get the ID for that season. Then I wont have to update every time, it will do it automatically.

Here is the call I make to get all the Seasons:

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


This is how I DD it right now:

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


And this is the result:

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"
}
]


How can I loop through this array and get the last index of this array to get the ID of the season.




This is how I make my call to get a particular playlist for this season:

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 ********************

Got it working by doing this:

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

Answer

Use end():

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

Or for earlier PHP versions:

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