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 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi