james
james

Reputation: 3

Loop over an associative 3 level array

How I can foreach array properly?

Code:

    while ($row = mysqli_fetch_assoc($result))
     {
      if ($use_sef_links == true)
      {
        $sef_link = "{$row['title']} ({$row['home_team_name']} vs {$row['away_team_name']}) {$row['dt']}";
        $sef_link = str_replace("-", " ", $sef_link);
        $sef_link = preg_replace('#\s{1,}#', " ", $sef_link);
        $sef_link = str_replace(" ", "-", $sef_link);
        $sef_link = preg_replace('#[^a-zA-Z0-9-]#', "", $sef_link);
        $sef_link .= '_m'. $row['id'] . '.html';

        $row['link'] = $sef_link;
      }
      else
      {
        $row['link'] = 'match.php?id=' . $row['id'];
      }

      $matches[$row['season_name']][$row['game_day']][] = $row;
     }
 foreach ($matches as $match) { ?>
        <section class="kode-pagesection margin-bottom-40" style="padding-top: 120px;">
          <div class="container">
            <div class="row">
                <div class="col-md-12">
                  <div class="kode-section-title"> <h2>Coming <?php echo $match['season_name']; ?> season game - <?php echo $match['game_day']; ?> gameday</h2> </div>
                  <div class="kode-fixer-list">
                    <ul class="table-head thbg-color">
                      <li> <h5>Coming games</h5></li>
                      <li> <h5>Game starts</h5> </li>
                      <li> <h5>Stadium</h5> </li>
                      <li> </li>
                    </ul>

                        <ul class="table-body">
                          <li>
                            <a href="#" class="list-thumb"><img src="<?php echo $match['home_team_logo']; ?>" class="circle-icon" style="height:50px; width:50px;" alt="<?php echo $match['home_team_name']; ?>"> <?php echo $match['home_team_name']; ?></a>
                            <span>VS</span>
                            <a href="#" class="list-thumb"><img src="<?php echo $match['away_team_logo']; ?>" class="circle-icon" style="height:50px; width:50px;" alt="<?php echo $match['away_team_name']; ?>"> <?php echo $match['away_team_name']; ?></a>
                          </li>
                          <li><small><?php echo $match['dt']; ?></small></li>
                          <li><small><?php echo $match['stadium']; ?></small></li>
                          <li class="fixer-btn">
                            <a href="<?php echo $match['link']; ?>"><?php echo $label_array[103]; ?></a>
                          </li>
                        </ul>
                  </div>
                </div>
              </div>
            </div>
        </section>
<?php } ?>

If I just print_r($matches); then I can see all seasons and games.. But I cant foreach matches as match..

I put image here to show what it shows if I print matches string.

Image: https://gyazo.com/ad2ebed217e0507cf315103b52273225

Upvotes: 0

Views: 41

Answers (2)

Jeff
Jeff

Reputation: 6943

I'd have written this as a comment, but there's not enough space....

Given the structure of your array $matches['season_name']['game_day'][]
I guess you want:

foreach($matches as $seasonName => $season) {

     // print the season-header
     echo $seasonName;

     foreach($season as $gamedayID => $gameday) {

         // print the gameday header
         echo $gamedayID;

         foreach($gameday as $match) {
             // print the details from the match
             echo $match['title'];
         }
     }
}

This is the version for grouping by season. See my comments for explanations!

Upvotes: 1

roxch
roxch

Reputation: 351

Too many unnecessary nesting though!

Btw you just need to use foreach (foreach array_expression as $key => $value) and array selector properly. For the inner arrays there'll be no advantage via using foreach, since you've got only one index array_expression as :

(foreach $matches as $matchIndex => $matchDetails){
            $currentMatch = $matchDetails[0];
            print_r($currentMatch);
            // or whatever you want to do with that
}

Upvotes: 0

Related Questions