【问题标题】:Undefinet offset error未定义的偏移错误
【发布时间】:2015-09-11 10:10:12
【问题描述】:

我的代码有时会给出代码未定义偏移错误。

Error: 
[03-Sep-2015 13:06:44] NOTICE: "Undefined offset: 6"

File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111

[04-Sep-2015 17:38:57] NOTICE: "Undefined offset: 8"

File: /home/mdmeds/public_html/includes/pages/game/class.ShowBuildingsPage.php | Line: 111

这是部分代码

$Element        = $CurrentQueue[$QueueID - 2][0]; /**this give the error*/
        $BuildEndTime   = $CurrentQueue[$QueueID - 2][3];
        unset($CurrentQueue[$QueueID - 1]);
        $NewQueueArray  = array();
        foreach($CurrentQueue as $ID => $ListIDArray)
        {               
            if ($ID < $QueueID - 1) {
                $NewQueueArray[]    = $ListIDArray;
            } else {
                if($Element == $ListIDArray[0] || empty($ListIDArray[0]))
                    continue;

                $BuildEndTime       += BuildFunctions::getBuildingTime($USER, $PLANET, $ListIDArray[0]);
                $ListIDArray[3]     = $BuildEndTime;
                $NewQueueArray[]    = $ListIDArray;             
            }
        }

我阅读了很多关于此类错误的文章,但我不知道如何修复我的代码。有人能帮助我吗 ?

【问题讨论】:

标签: php undefined offset


【解决方案1】:

您正在尝试使用您甚至不确定它们是否存在的索引...

类似的东西

$CurrentQueue[$QueueID - 2]

是一个猜测...找到另一种方式..

【讨论】:

    【解决方案2】:

    在这段代码$CurrentQueue[$QueueID - 2][0]中,密钥是根据$QueueID动态生成的。当您收到错误时,表示指定的键在数组$CurrentQueue中不可用。

    为避免此类运行时异常,您可以执行以下操作

    if (!empty($CurrentQueue[$QueueID - 2])) {
      // the actual functionality goes here.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多