处理此数组时,每次引用数组时都会发出通知,直到添加某些数据。无论如何,所有输出都按预期工作。
通知的屏幕截图: 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();
是否应禁用通知或是否有更好的方法来解决此问题?
我尝试填充它,但代码最终变成了两倍且非常混乱。
答案 0 :(得分: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());
}