From: remy215 at laposte dot net
Operating system: debian
PHP version: 5.2.4RC1
PHP Bug Type: Arrays related
Bug description: unwanted array reset within recursive call
Description:
------------
I have an array of nested arrays (ie. each id can have nested children).
The function returns the parent of the provided id but end in an infinite
loop when the id does not exists !
Parent array at the top of the hierarchy seems to undergo an unwanted
reset();
Reproduce code:
---------------
<?php
$array=array('a0'=>array('a1'=>array('a2'=>array(),'b2'=>array()),'b1'=>array()),'b0'=>array());
$searched=array(); // to avoid infinite loop
function getParent($id,$_subtree=null) {
if(!$_subtree) { // if first call
global $array;
$_subtree=$array; // entire tree
}
global $searched; // to avoid infinite loop
$found_parent=null;
foreach($_subtree as $parent=>$children) {
if(in_array($parent,$searched)) { // if already looped => stop
(ie.
infinite loop)
die('Infinite loop: '.$parent);
}
array_push($searched,$parent); // to avoid infinite loop
if(in_array($id,array_keys($children))) {
$found_parent=$parent;
break;
} elseif($found_parent=getParent($id,$children)) {
break;
}
}
return $found_parent;
}
$search='a0';
echo 'parent of '.$search.' is: '.getParent($search);
?>
Expected result:
----------------
When you provide as argument a nested id, the function works ('b2' returns
'a1').
When you provide a non-existing id, or one of the top parent ids ('a0' or
'b0') the function should return null.
Actual result:
--------------
When you provide a non-existing id, or one of the top parent ids ('a0' or
'b0') the function end up in an infinite loop.
Parent array at the top of the hierarchy seems to undergo an unwanted
reset();
--
Edit bug report at http://bugs.php.net/?id=42236&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=42236&r=trysnapshot44
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=42236&r=trysnapshot52
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=42236&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=42236&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=42236&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=42236&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=42236&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=42236&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=42236&r=support
Expected behavior: http://bugs.php.net/fix.php?id=42236&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=42236&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=42236&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=42236&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=42236&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=42236&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=42236&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=42236&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=42236&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=42236&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=42236&r=mysqlcfg