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

时间:2014-12-08 13:16:27

标签: php nested-loops

我有八个社交网络每周末在四个球场上打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比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再次运行;如果剧本未能找到最后一个法院使用。

或者有更聪明的解决方案吗?我应该创建一个调用循环的函数吗?我可以在其中称呼它吗?

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

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西。不需要递归函数。在底部,您可以找到所使用的重要功能的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";
}
相关问题