从JSON数组中获取最后一个数组索引 - laravel 5.2

时间:2016-05-04 18:59:44

标签: php arrays json laravel-5.2

我有一个Halo 5 API的排行榜,对于每个排行榜,我有一个特定播放列表的前100名用户,本赛季。我遇到的问题是我必须打电话来获取这些数据,这就是它的要求:

// $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都会发生变化,因为新赛季开始了。所以我必须每月更新一次,效率不高。

我需要循环遍历季节数组并获取该循环的最后一个数组索引,以便我可以获得该季节的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;
    }

/ *************编辑********************

通过这样做得到它:

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

1 个答案:

答案 0 :(得分:1)

使用end()

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

或者早期的PHP版本:

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