David
David

Reputation: 2007

Loop through JSON array to get score - Laravel 5.2

I'am having problem looping through an array to get every teams score for the last 10 games played by a player. This is how I'm looping through this array:

       $WarzoneLast10MatchesTeamScore = [];
        foreach($warzoneLast10matches->Results->Teams as $idx => $stats){
            $WarzoneLast10MatchesTeamScore[$idx]['Score'] = $stats->Score;
            $WarzoneLast10MatchesTeamScore[$idx]['Id'] = $stats->Id;
        }

The problem with this is it will give me an error because I'am trying to get the last 10 games played, so result has to be then Result[0], Result[1], and so on. Here is what I mean:

+"Results": array:10 [▼
    0 => {#17371 ▼
      +"Links": {#13129 ▶}
      +"Id": {#13130 ▶}
      +"HopperId": "0e39ead4-383b-4452-bbd4-babb7becd82e"
      +"MapId": "c89dae21-f206-11e4-a1c2-24be05e24f7e"
      +"MapVariant": {#13121 ▶}
      +"GameBaseVariantId": "42f97cca-2cb4-497a-a0fd-ceef1ba46bcc"
      +"GameVariant": {#17372 ▶}
      +"MatchDuration": "PT6M50.2813116S"
      +"MatchCompletedDate": {#17367 ▶}
      +"Teams": array:2 [▼
        0 => {#17374 ▼
          +"Id": 0
          +"Score": 1
          +"Rank": 1
        }
        1 => {#17375 ▼
          +"Id": 1
          +"Score": 0
          +"Rank": 2
        }
      ]
      +"Players": array:1 [▶]
      +"IsTeamGame": true
      +"SeasonId": null
      +"MatchCompletedDateFidelity": 1
    }
    1 => {#17378 ▶}
    2 => {#17390 ▶}
    3 => {#17402 ▶}
    4 => {#17414 ▶}
    5 => {#17426 ▶}
    6 => {#17438 ▶}
    7 => {#17450 ▶}
    8 => {#17462 ▶}
    9 => {#17474 ▶}
  ]

I obviously don't want to make 10 loops for each game then hard code the score for each game in my view. How can I loop through the Results object, and then get the Teams->score and Teams->Id objects?

FYI I know I can use collections like this:

public function getWarzoneLast10Matches($warzoneLast10matches) {

        // Collect al the results for this array
        $results = collect($warzoneLast10matches->Results);

        $array = $results->map(function($item, $key) {
                return [
                    'Gamertag' => $item->Players[0]->Player->Gamertag,
                    'MapId' => $item->MapId,
                    'GameBaseVariantId' => $item->GameBaseVariantId,
                    'Score' => $item->Teams[0]->Score,
                    'Score2' => $item->Teams[1]->Score,
                    'Id' => $item->Teams[0]->Id,
                    'Id2' => $item->Teams[1]->Id,
                ];
            });


        return $array;
    }

But this will not work because in some games there is only 1 team, and if that happens, then it will throw me an error saying undefined offset 1, because there is no team 2. The other method I'am using on top wont give me errors.

Upvotes: 0

Views: 129

Answers (1)

Marc Barbeau
Marc Barbeau

Reputation: 836

It sound like you might need to make your function recursive.

Learn more about Recursion

Upvotes: 0

Related Questions