ID: 42236
User updated by: remy215 at laposte dot net
Reported By: remy215 at laposte dot net
-Status: Feedback
+Status: Open
Bug Type: Arrays related
Operating System: debian
PHP Version: 5.2CVS-2007-08-12
New Comment:
I've experienced it 5.2.0 too so far.
Regarding the use of 'global': in fact this function was a method of a
class and the global variable was not 'global' but a property of that
same class.
I removed all this class wrapper just for this post => this is why I
used 'global' to simulate the corresponding class property.
In any case, no worries at all since I've a workaround.
Regards,
remy
Previous Comments:
------------------------------------------------------------------------
[2007-08-16 11:19:05] [EMAIL PROTECTED]
Now I just want one thing cleared: Did you experience this with earlier
PHP versions than 5.2.3? As I'm quite sure it has been like this always.
And using global when not really necessary is always bad..
------------------------------------------------------------------------
[2007-08-15 23:31:55] remy215 at laposte dot net
I 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
------------------------------------------------------------------------
[2007-08-15 15:46:41] [EMAIL PROTECTED]
Try replace the foreach() calls with while/each struct...foreach() does
a reset on the array..
------------------------------------------------------------------------
[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..
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/42236
--
Edit this bug report at http://bugs.php.net/?id=42236&edit=1