0

处理此数组时,每次引用该数组时都会给出通知,直到向其中添加某些内容。无论如何,所有输出都按预期工作。

通知截图:http: //imgur.com/75RGA

function create_round(&$arrTeam)
{
    $numTeams = 4;

    $used = array();

    for ($i = 0; $i < $numTeams; $i++) {
        if (!in_array($i, $used)) { //if i isnt already scheduled to play
            for ($u = $numTeams-1; $u > $i; $u--) {
                if (!in_array($u, $used) && !in_array($u, $arrTeam[$i]["games"])) { //if u isnt already scheduled to play and u hasnt been played by i before
                    $arrTeam[$i]["games"][sizeof($arrTeam[$i]["games"])] = $u;
                    $arrTeam[$u]["games"][sizeof($arrTeam[$u]["games"])] = $i;

                    $used[sizeof($used)] = $i;
                    $used[sizeof($used)] = $u;

                    echo($arrTeam[$i]["name"] . " VS " . $arrTeam[$u]["name"] . "<br>");
                    break;
                }
            }   
        }
    }

    var_dump($arrTeam);
}

function round_robin()
{
    $numTeams = 4;

    //Create array
    $arrTeam = array(
        $team = array(
            "name" => "",
            "games" => array()
        )
    );

    //TEMP ASSIGNING NAMES
    for ($i = 1; $i < $numTeams+1; $i++)
        $arrTeam[$i-1]["name"] = "Team $i"; 

    //Echo Round numbers
    for ($i = 1; $i < $numTeams; $i++) {
        echo("<br>Round $i<br>");
        create_round($arrTeam);
    }
}

round_robin();

应该禁用通知还是有更好的处理方法?

我尝试填充它,但代码长度增加了一倍而且非常混乱。

4

1 回答 1

0

你到处检查这个:

$arrTeam[$i]["games"]

没有测试它的存在。改成:

if (isset($arrTeam[$i]["games"]) && ..more checking.. ) { ..do stuff.. }

或者您可以禁用通知:

error_reporting(E_ALL ^ E_NOTICE);

编辑:

在 round_robin() 函数中执行一次:

代替:

$numTeams = 4;

//Create array
$arrTeam = array(
    $team = array(
        "name" => "",
        "games" => array()
    )
);

做:

$numTeams = 4;
for($x=0;$x<$numTeams;$x++) {
   $arrTeam[$x] = array("name"=>"","games"=>array());
}
于 2012-06-06T12:48:59.013 回答