0

My code sometimes give code undefinet offset error.

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

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

and

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

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

This is the part of the code

$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;             
            }
        }

I read lot of articles about this kind of errors but i do not know how to fix my code. Can someone help me please ?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Maaverick
  • 63
  • 1
  • 8
  • possible duplicate of [PHP Notice: Undefined offset: 1 with array when reading data](http://stackoverflow.com/questions/17456325/php-notice-undefined-offset-1-with-array-when-reading-data) – Abdulla Nilam Sep 11 '15 at 10:22

2 Answers2

0

You are trying to play with indexes that you are not sure they even exist...

things like

$CurrentQueue[$QueueID - 2]

is a guess... Get to find another way..

Julio Soares
  • 1,200
  • 8
  • 11
0

In this piece of code $CurrentQueue[$QueueID - 2][0], the key is generated dynamically based on $QueueID. WHen you get the error, it means that the specified key is not available in the array $CurrentQueue.

To avoid such run time exceptions, you can do something like this

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