如何在数组内查找数组

时间:2018-06-19 20:10:55

标签: php

我如何正确地foreach数组?

代码:

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

如果我只是print_r($ matches);然后我可以看到所有季节和比赛。 但是我不能让每场比赛都像火柴一样。

我将图像放在此处以显示如果我打印匹配字符串所显示的内容。

图片: https://gyazo.com/ad2ebed217e0507cf315103b52273225

2 个答案:

答案 0 :(得分:1)

本来可以写为评论,但是没有足够的空间。...

给出数组$matches['season_name']['game_day'][]
的结构 我猜你想要:

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

这是按季节分组的版本。 请参阅我的评论以获取解释!

答案 1 :(得分:0)

不过有太多不必要的嵌套!

最后,您只需要正确使用foreach(foreach array_expression as $key => $value)和数组选择器即可。对于内部数组,使用 foreach 没有任何优势,因为您只有一个索引array_expression为:

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