dongxianghui3709 2012-06-06 12:41
浏览 12
已采纳

处理具有空尺寸的多维数组

When dealing with this array, notices are given each time the array is referenced until something is added to it. All of the output works as expected regardless.

A screenshot of the notices: 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();

Should notices just be disabled or is there a better way of dealing with this?

I tried padding it but the code ended up twice as long and very messy.

  • 写回答

1条回答 默认 最新

  • doushouhe7072 2012-06-06 12:48
    关注

    you're checking this one all over the place:

    $arrTeam[$i]["games"]
    

    without testing its existence. Change to:

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

    or you can disable notices:

    error_reporting(E_ALL ^ E_NOTICE);
    

    EDITED:

    do this once in round_robin() function:

    instead of:

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

    do:

    $numTeams = 4;
    for($x=0;$x<$numTeams;$x++) {
       $arrTeam[$x] = array("name"=>"","games"=>array());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么