-1

I have two foreach loops, the outer loop works just the way I want it to. I however, want inner loop to display a specific number of rows after which it should stop/break without affecting the the outer loop.

Am trying to execute loop as below.

DATA SOURCE SAMPLE

[
    {
        "season": "2019",
        "date": "2019-03-01",
        "league_id": "1979",
        "league": "Chinese Super League",
        "team1": "Shandong Luneng",
        "team2": "Guizhou Renhe",
        "spi1": "48.22",
        "spi2": "37.83",
        "prob1": "0.5755",
        "prob2": "0.174",
        "probtie": "0.2505",
        "proj_score1": "1.75",
        "proj_score2": "0.84",
        "importance1": "45.9",
        "importance2": "22.1",
        "score1": "1",
        "score2": "0",
        "xg1": "1.39",
        "xg2": "0.26",
        "nsxg1": "2.05",
        "nsxg2": "0.54",
        "adj_score1": "1.05",
        "adj_score2": "0.0"
    },
    {
        "season": "2019",
        "date": "2019-03-01",
        "league_id": "1979",
        "league": "Chinese Super League",
        "team1": "Guangzhou Evergrande",
        "team2": "Tianjin Quanujian",
        "spi1": "65.59",
        "spi2": "39.99",
        "prob1": "0.7832",
        "prob2": "0.0673",
        "probtie": "0.1495",
        "proj_score1": "2.58",
        "proj_score2": "0.62",
        "importance1": "77.1",
        "importance2": "28.8",
        "score1": "3",
        "score2": "0",
        "xg1": "0.49",
        "xg2": "0.45",
        "nsxg1": "1.05",
        "nsxg2": "0.75",
        "adj_score1": "3.15",
        "adj_score2": "0.0"
    },] //json continues for other leagues and games....

The LOOP

$group = false;
foreach(array_reverse($return, true) as $match) { 
    if ($date == $match->date) { 
            // $date variable is defined somewhere, no worries
            if($group != $match->league) {
                echo '<div class="league_name">'. $match->league . "</div>";
            }
            $group = $match->league;
            // do stuff for first foreach working fine here
            echo $match->team1;
            echo $match->team2;

    // Edited/the answer.
    $i = 1;
    foreach($return as $team) {
        if ($team->team1 == $match->team1 && is_numeric($team->score1)) {
          // display goals scored in each match by team1 in the last 5 matches
          echo $team->score1."<br />"; 
          if ($i++ == 5) {
          // and break after 5
             break; 
          }
      }
   }

} if($group !== false); 

EDIT

Why have if($group !== false);.? I used it to group the games/fixtures by league e.g Chinese Super League, Barclays Premier League etc.. I want the second loop to show (only 5) e.g 2 6 3 0 4 and then have them displayed as below.

Chinese Super League
  Shandong Luneng      vs     Guizhou Renhe
  2 6 3 0 4  /* 2nd loop */   1 0 1 1 0
  Guangzhou Evergrande vs     Tianjin Quanujian
  5 1 3 0 1  /* 2nd loop */   4 0 4 3 0
Barclays Premier League
  Fulham               vs     Liverpool
  5 0 0 0 1  /* 2nd loop */   1 0 1 3 2
  AFC Bournemouth      vs     Aston Villa
  2 3 3 1 1   /* 2nd loop */  4 0 2 3 0
  Leeds United         vs     Wolverhampton
  1 1 4 1 1   /* 2nd loop */  1 0 2 0 0

But it instead it shows me (more than 5) 3 0 1 5 1 4 1 0 1 1 0 2 2 6 3 0 4

NOTE

The five(5) 2 6 3 0 4 are the last rows.

Daniel Omara
  • 117
  • 2
  • 17
  • 1
    You have several `if` statements with no body. What are `if ($i++ == 5);` and `if($group !== false);` for? – Barmar May 22 '23 at 21:18
  • By default `break` breaks the innermost loop. `break 2` breaks the next outer loop, `break 3` breaks 3 levels out, and so on. – Barmar May 22 '23 at 21:19
  • Why do you use the `preserve_keys = true` in `array_reverse()` when you're not accessing the keys? – Barmar May 22 '23 at 21:20
  • Check this: https://stackoverflow.com/questions/9215588/break-out-of-if-and-foreach – Kauno Medis May 22 '23 at 21:21
  • Also, for a `break` statement to function it needs to actually _exist_ in your code. – Sammitch May 22 '23 at 21:25
  • @Barmar Made some edits to elaborate why I used the `if($group !== false); ` and since the JSON data starts from old date to recent date, I thought I would use the `array_reverse()` to start with the most recent game of the (5 games) as the first row and not the last row. Thank you – Daniel Omara May 23 '23 at 14:46
  • But you're not doing anything when `$group !== false`. It should be `if ($group !== false) { some code here }` – Barmar May 23 '23 at 14:48
  • The same thing with `if ($i++ == 5);` – Barmar May 23 '23 at 14:48

2 Answers2

-1

Put the if statement inside the foreach. When it breaks, it will just break that loop.

There's no need to increment $i yourself. Just use the array indexes supplied by foreach

        foreach($return as $i => $team) {
            if ($team->team1 == $team1 && is_numeric($team->score1)) {
                // display goals scored in each match by team1 in the last 5 matches and break/stop
                echo $team->score1."<br />";;
            }   
            if ($i == 5) {
                break;
            }
        } 
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

Got it finally working

Put $i = 1 outside the second/inner loop and the if($i++ == 5) { break;} statement inside the if statement.

    $i = 1;
    foreach($return as $team) {
        if ($team->team1 == $match->team1 && is_numeric($team->score1)) {
           // display goals scored in each match by team1 in the last 5 matches
           echo $team->score1."<br />"; 
        if ($i++ == 5) {
            // and break after 5
            break; 
            }
         }
    }  
Daniel Omara
  • 117
  • 2
  • 17