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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图