在 foreach 循环中跳过一行

我创建了一个滑块来显示篮球比赛列表。我已经让它工作了,目前在我的网站上看起来像这样:

https://img1.sycdn.imooc.com/6535249a00018be106540069.jpg

我使用以下 foreach 循环来显示数据。


    <?php 

    

    uasort($json['response']['data'], function ($a, $b) {

        if ($a['matchTime']==$b['matchTime']) return 0;

        return $a['matchTime']>$b['matchTime'] ? 1 : -1;

        });


    foreach($json['response']['data'] as $item) {

        if ($item['competitionName'] == 'Junior SS Premiership Zone 3' ) {

            continue;}

        if ($item['competitionName'] == 'CSW Division 1' ) {

        continue;}

        if ($item['competitionName'] == 'Season Showdown 19' ) {

        continue;}

        if ($item['competitionName'] == 'SS19 Challengers' ) {

        continue;}

        if ($item['competitionName'] == 'SS20 Challengers' ) {

        continue;}

        if ($item['competitionName'] == 'SS20 Pre-Season' ) {

        continue;}

        if ($item['competitionName'] == 'Schick AA Zone 3 Premierships' ) {

        continue;}

        if ($item['competitionName'] == 'New World Series 2018' ) {

        continue;}

        if ($item['competitionName'] == 'SS18 Challengers' ) {

        continue;}

    

        print'

        

        <li class="item">

                        <a class="item-link" href="/schedule">

                            <div class="game-meta game-date date-format">

                                <span class="date-month">'; $matchtime = $item['matchTime']; echo date('M', strtotime($matchtime)); print'</span>'; $matchtime = $item['matchTime']; echo date('j', strtotime($matchtime));

                            print '</div>';

                            

                           

它运行良好。我得到了数据,但我的 foreach 循环显示了<div class="game-meta game-vs">vs</div>foreach 应该执行的操作的两次。我想删除它,所以它只显示在中间。现在,图像显示了 TEAM 与 TEAM 的对比,它应该只显示 TEAM 与 TEAM 的对比。


有办法做到这一点吗?我尝试将其设置为 continue 下的变量,但它不喜欢它。


慕婉清6462132
浏览 131回答 2
2回答

蛊毒传说

似乎只有两支球队在比赛,所以只需将代码更改为:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($item["competitors"] as $teams){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-meta game-team">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="game-team__logo game-team__logo-tc" alt="Tawa" src="https://summerseries.nz/wp-content/themes/SS/logos/'; echo $teams['teamCode']; print '.svg">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-team__abbrev">'; echo $teams['teamCode']; print'</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-meta game-vs">vs</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };到:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-meta game-team">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="game-team__logo game-team__logo-tc" alt="Tawa" src="https://summerseries.nz/wp-content/themes/SS/logos/'; echo $item["competitors"][0]['teamCode']; print '.svg">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-team__abbrev">'; echo $item["competitors"][0]['teamCode']; print'</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-meta game-vs">vs</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-meta game-team">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="game-team__logo game-team__logo-tc" alt="Tawa" src="https://summerseries.nz/wp-content/themes/SS/logos/'; echo $item["competitors"][1]['teamCode']; print '.svg">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-team__abbrev">'; echo $item["competitors"][1]['teamCode']; print'</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>';如果有超过 2 支球队进行比赛,请告诉我,我会更改解决方案祝你有美好的一天~

largeQ

计算您已经执行了多少次循环,<div>如果不是最后一次迭代,则仅打印“vs”:$i = 0; // How many teams we've printed so farforeach ($item["competitors"] as $teams){&nbsp; &nbsp; print '&nbsp; &nbsp; &nbsp; &nbsp; <div class="game-meta game-team">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="game-team__logo game-team__logo-tc" alt="Tawa" src="https://summerseries.nz/wp-content/themes/SS/logos/'; echo $teams['teamCode']; print '.svg">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="game-team__abbrev">'; echo $teams['teamCode']; print'</div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; ';&nbsp; if ( ++$i < sizeof($item["competitors"]) ) {&nbsp; &nbsp; print '&nbsp; &nbsp; &nbsp; &nbsp; <div class="game-meta game-vs">vs</div>&nbsp; &nbsp; ';&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP