From:             jxmaster at msn dot com
Operating system: windowsxp
PHP version:      4.3.11
PHP Bug Type:     Arrays related
Bug description:  foreach fails to continue after returning from a recursion 
call

Description:
------------
'foreach' fails to continue visiting the following items in the array
after returning from a recursion function call. I understand that
'foreach' works on the copy of the array, but I think it cannot explain
the situation. Because after returning from the recursion call, the memory
space before the call should be popped out from the running enviorenment
stack. Hence, foreach should not feel that its execution has been
interrupted by the recursion call.
If I change the structure of the array slightly and use for loop to
replace foreach the function get the expected value. 
Following is the revised version:
function recurGetDescdendant($all, $current, $descendant) {
 $length = count($all);
 for ($i = 0; $i < $length; $i++) {
        $userid = $all[$i][0];
        $parent = $all[$i][1];
        if ($parent == $current) {
    $descendant[] = $userid;
    $cdescendant = array();
    recurGetDescdendant(&$all, $userid, &$cdescendant);
    $descendant = array_merge($descendant, $cdescendant);
  }  }  }
//key represents the user id; value reprents the parent's user id of this
user. for example: 6 => 1 means user 6's parent is user 1
  $all = array(array(1,0), array(2,0), array(3,0), array(4,0), array(5,0),
array(6,1), array(7,1), array(11,7), array(12,11), array(13,11) );
  $descendant = array();
  recurGetDescdendant(&$all, 1, &$descendant);
  foreach ($descendant as $value) echo $value . "<br>\n";

Reproduce code:
---------------
function recurGetDescdendant($all, $current, $descendant) {
 foreach ($all as $userid => $parent) {
   if ($parent == $current) {
    $descendant[] = $userid;
    $cdescendant = array();
    recurGetDescdendant(&$all, $userid, &$cdescendant);
    $descendant = array_merge($descendant, $cdescendant);
  }  }  }
//key represents the user id; value reprents the parent's user id of this
user. for example: 6 => 1 means user 6's parent is user 1
  $all = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 1, 7 => 1, 11
=> 7, 12 => 11, 13 => 11 );
  $descendant = array();
  recurGetDescdendant(&$all, 1, &$descendant);
  foreach ($descendant as $value) echo $value . "<br>\n";

Expected result:
----------------
6<br>
7<br>
11<br>
12<br>
13<br>

Actual result:
--------------
6<br>

-- 
Edit bug report at http://bugs.php.net/?id=33031&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=33031&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=33031&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=33031&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=33031&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=33031&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=33031&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=33031&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=33031&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=33031&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=33031&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=33031&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=33031&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=33031&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=33031&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=33031&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=33031&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=33031&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=33031&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=33031&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=33031&r=mysqlcfg

Reply via email to