|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-08-07 17:21 UTC] 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();
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 02:00:01 2025 UTC |
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, RemyI know that foreach reset the array. In fact I am not looking for a workaround, I already have one. It's just that the behavior of php looks quite unexpected to me and it could be a bug. Normally, calling << getParent('a0'); >> or calling << getParent('a0',$array); >> should lead exactly to the same result since when the second argument is not provided, it defaults to $array. But that's not the case, the first call leads to an infinite loop while the second delivers the expected result (null). I hope I am not wrong. In any case, thanks for your help. Regards, Remy