开发者

PHP Notice: Undefined offset: -1, while loop and PHP Fatal error: Out of memory

Well, as in the title, I am having a problem due to those things. The problem is occurring due to line X which is while ($right[count($right)-1]<$row['rgt']) { and this is in a function display_tree from SitePoint's Tree Traversal.

The function was working well, but I don't know why it has suddenly started to throwing this fatal error.

I tried 开发者_开发百科using error_reporting(-1); to understand what might be causing the error, and the new error log shows me that I'm getting the PHP Notice multiple time, as if in an unfinished loop, till a point where I am get that Out of memory error.

The weird thing is this was working perfectly till two days ago, since when I am pulling my hairs out to decipher what is causing the problem.

Any way to understand what is causing the problem exactly? or may be some other helpful tips?

Here is the while loop inside it's condition:

if (count($right)>0) {
$j=0;
while ($right[count($right)-1]<$row['rgt']) {  
  array_pop($right);
   $j++;
  }   
}

Thanks guys.


For starters, the notice Undefined offset: -1 suggests that array $right is empty.

Edit: In your loop you're popping $array down to nothing... guaranteed to fail. Need to stop the loop before the array becomes empty.

This will solve the immediate problem, but it's unlikely (by itself) to make your program work:

while ($right && $right[count($right)-1]<$row['rgt']) {

Since end($right) returns the same value $right[count($right)-1], we can simplify this to:

while ($right && end($right) < $row['rgt']) {


If you are following Site Points function completely, I imagine the reason for the error is that your tree is corrupt.

Even more reason to suggest your tree is corrupt, is that you say the code was working fine a few days ago...

I've testing your function on the Site Point tree and it seems to run fine.

The function doesn't do any checking for a corrupt tree. The while loop condition you first pointed out should never reach a situation where $right is empty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜