如果结果不是我想要的,则重做嵌套循环


Redo nested loop if result is not what i wanted

我有八支足球队,每个周末在四个球场上打1x1。我想完成的是随机设置球场,但任何球队的球场都不允许与前一场比赛相同。

我为每一轮都有一个 foreach 循环,其中包含该轮中每个游戏的 foreach 循环。

问题是我随机设置了球场,在一个回合的最后一场比赛中,剩下的一个球场并不总是按照指定的规则工作。因此,我需要为当前轮次中的每场比赛重试foreach循环。我该怎么做?我需要以不同的方式处理这个问题吗?

以下是赛季第二轮的示例:

主队上场: 1
客队上场:2
球队: 2 对 4
法院: 3

这些法院已经在使用:3,
主队上场: 3
客队上场: 1
球队: 6 对 1
球场: 2

这些法院已经在使用:3、2、
主队上场: 4
客队上场:2
球队: 8 对 3
法院: 1

这些法院已经在使用:3、2、1、
主队上场: 4
客队上场: 3
球队: 7 vs. 5
法院:没有可能的替代方案

代码示例

$rounds = SQL FOR SELECTING ALL ROUNDS;
$teams = array();
$i = 0;
$prevround = '';
$reload = false;
foreach($rounds as $round) {
    $roundid = $round->ROUND;
    $matches = "SELECT * FROM TABLE WHERE ROUND = '$roundid'";
foreach($matches as $match) {
    $i++;

    $round = $match->ROUND;
    // If new round (could possibly be placed above foreach instead)
    if($round != $prevround) {
        echo "<h2>Round nr. ".$match->ROUND."</h2><br />";
        $courtarray = array();
        $prevround = $match->ROUND;
        $clearcache = false;
        $prevcourt = 0;
    }

    $stop = false;
    echo "These courts are already in use:";
    foreach($courtarray as $bannummer) {
        echo $bannummer.", ";
    }
    echo "<br>";
    echo "The home teams previous court: ".$teams[$match->TEAM_HOME]."<br>";
    echo "The away teams previous court:".$teams[$match->TEAM_AWAY]."<br>";
    //for($j=1;$j<5;$j++) {
    $k = 0;
    back:
    $j = rand(1,4);
        //Check if the random number (1-4) can match the rules
        if($j == $teams[$match->TEAM_HOME] or $j == $teams[$match->TEAM_AWAY] or in_array($j, $courtarray)) {
            //Try this 10 times
            $k++;
            if($k > 10) {
                $reload = true;
            } else {
                // Yes, i know this is not a nerdy way to do it :)
                goto back; 
            }
        } else if($stop != true) {
            $court = $j;
            $bcourtarray[] = $court;
            $stop = true;
        }
    //}

    echo "<strong>Teams: ".$match->TEAM_HOME." - ".$match->TEAM_AWAY."<br />";
    if($court == $prevcourt) {
        echo "Court: NO POSSIBLE ALTERNATIVES</strong><br />";
        $startover = true;
    } else {
        echo "Court: ".$court."</strong><br />";
    }
    // Used to save the different team´s previous court
    $teams[$match->TEAM_HOME] = $court;
    $teams[$match->TEAM_AWAY] = $court;
    $prevcourt = $court;
}

}

所以我想我需要制作

 $matches = $db2->get_results("SELECT * FROM TABLE WHERE ROUND = '$roundid'");
 foreach($matches as $match) {

以当前$roundid再次运行;如果脚本找不到要使用的最后一个法院。

或者有更聪明的解决方案吗?我应该创建一个调用循环的函数吗?我可以称之为本身吗?

有时你会陷入某种思维方式。 :)

我认为这就是你要找的。不需要递归函数。在底部,您可以找到指向所用重要函数的 PHP 参考的链接。
我正在做的是跟踪每个团队使用的球场($usedCourts[__TEAM__](,并查看是否有任何球场($courts(没有被使用array_diff()的两支球队占用。我使用以下array_shift()选择第一个可用的法院:

$numberOfCourts = 3;


$courts = range(1,$numberOfCourts);
$usedCourts = array();
foreach($matches as $match) {
    if(!array_key_exists($match->TEAM_HOME, $usedCourts)) {
        $usedCourts[$match->TEAM_HOME] = array();
    }
    if(!array_key_exists($match->TEAM_AWAY, $usedCourts)) {
        $usedCourts[$match->TEAM_AWAY] = array();
    }
    $possibleCourts = array_diff($courts, $usedCourts[$match->TEAM_HOME], $usedCourts[$match->TEAM_AWAY]);
    echo $match->TEAM_HOME . ' - ' . $match->TEAM_AWAY;
    if(count($possibleCourts)) {
        $currentCourt = array_shift($possibleCourts);
        $usedCourts[$match->TEAM_HOME][] = $currentCourt;
        $usedCourts[$match->TEAM_AWAY][] = $currentCourt;
        echo ' on court: ' . $currentCourt;
    } else {
        echo ' has no available field';
    }
    echo "'n";
}
  • range() - 手动
  • array_diff() - 手动
  • array_key_exists() - 手动
  • array_shift() - 手动