skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

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

  2. 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;
                }
            } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search