dongxian3418 2018-06-19 20:10
浏览 29
已采纳

如何foreach阵列内的数组

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

  • 写回答

2条回答 默认 最新

  • doufeikuang7724 2018-06-19 20:32
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?