#42236 [Opn->Bgs]: unwanted array reset within recursive call

From: Date: Thu, 16 Aug 2007 14:00:21 +0000
Subject: #42236 [Opn->Bgs]: unwanted array reset within recursive call
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-116216@lists.php.net to get a copy of this message
 ID:               42236
 Updated by:       jani@php.net
 Reported By:      remy215 at laposte dot net
-Status:           Open
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: debian
 PHP Version:      5.2CVS-2007-08-12
 New Comment:

Apparently it isn't a workaround but how it's supposed to be done.


Previous Comments:
------------------------------------------------------------------------

[2007-08-16 12:54:27] remy215 at laposte dot net

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

------------------------------------------------------------------------

[2007-08-16 11:19:05] jani@php.net

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] jani@php.net

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

------------------------------------------------------------------------

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


Thread (10 messages)

« previous php.bugs (#116216) next »