ID: 42236 Updated by: [EMAIL PROTECTED] Reported By: remy215 at laposte dot net -Status: Open +Status: Feedback Bug Type: Arrays related Operating System: debian PHP Version: 5.2CVS-2007-08-12 New Comment:
Try replace the foreach() calls with while/each struct...foreach() does a reset on the array.. Previous Comments: ------------------------------------------------------------------------ [2007-08-15 12:47:01] remy215 at laposte dot net Hi Jani, First thank you for your support. Here is a cleaner version (without the infinite-loop checking): <?php $array=array( 'a0'=>array( 'a1'=>array( 'a2'=>array(), 'b2'=>array() ),'b1'=>array() ),'b0'=>array() ); function getParent($id,$_subtree=null) { $found_parent=null; if(!$_subtree) { global $array; $_subtree=$array; } foreach($_subtree as $parent=>$children) { if(in_array($id,array_keys($children))) { $found_parent=$parent; break; } elseif($found_parent=getParent($id,$children)) { break; } } return $found_parent; } echo "\nparent of b2 is: ".getParent('b2'); // expected result: 'a1' -- actual result: 'a1' echo "\nparent of a0 is: ".getParent('a0'); // expected result: null -- actual result: infinite loop ?> What's really strange is that if you call getParent('a0',$array), it works perfectly ! it's the [global $array; $_subtree=$array;] used for initialization that triggers an infinite loop. Regards, Remy ------------------------------------------------------------------------ [2007-08-15 08:44:39] [EMAIL PROTECTED] Please provide the simplest possible script that shows exactly what is going wrong. The current example is too complex and seems to contain all kinds of useless workarounds.. ------------------------------------------------------------------------ [2007-08-12 21:36:09] remy215 at laposte dot net I tested with latest cvs snapshot, still unexpectingly reseting the array, leading to an unpredictable infinite loop. ------------------------------------------------------------------------ [2007-08-07 17:21:17] remy215 at laposte dot net 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 this bug report at http://bugs.php.net/?id=42236&edit=1