ID:               22879
 User updated by:  lev at centers dot ru
 Reported By:      lev at centers dot ru
-Status:           Bogus
+Status:           Open
 Bug Type:         Scripting Engine problem
 Operating System: FreeBSD 4.6
 PHP Version:      4.3.1
 New Comment:

"Nested foreach" and "recursive function call into foreach loop" is
deferent things.
I guess: control variable of foreach loop is global and corrupted after
recursive function call.

Compact variant of example:
--------------------------
<?
// Bag: recursive function corrupt foreach loop
  function out_node($k,$level)
  {
    global $name,$ind;
    for($i=0;$i<$level;$i++) echo "&nbsp;&nbsp;&nbsp;";
    echo "$name[$k]<BR>\n";
    foreach($ind as $i=>$index) {
      if($index==$k) out_node($i,$level+1);
    }
  }
?>
<html>
<head>
</head>
<body>
<?
  $name=Array('node1','leaf1','leaf2','node2','leaf3','leaf4');
  $ind=Array('-1','0','0','0','3','3');
  out_node(0,0);
?>
</body>
</html>

Result:
-------
node1
   leaf1

must be (Correct result):
------------------------
node1
   leaf1
   leaf2
   node2
      leaf3
      leaf4


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

[2003-03-25 17:01:48] [EMAIL PROTECTED]

Your example is far to complex so i made it bogus. Try stripping it
down and reopen the bug if you really think it is a PHP bug and not a
bug in your script.

The only thing that came to my mind beeing able to cause
an error here is the fact you are using foreach in a nested
way. So i created a really short test for that and it works with both
PHP 4.3.2 and PHP 5-dev.

[EMAIL PROTECTED] php4-HEAD]$ ../PHP_4_3_0/sapi/cli/php -r
'$ar=array(0,1,2);foreach($ar as $n) foreach($ar as $m) echo
"$n/$m\n";'
0/0
0/1
0/2
1/0
1/1
1/2
2/0
2/1
2/2

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

[2003-03-25 10:14:19] lev at centers dot ru

<?
// Bag: recursive function corrupt foreach loop
// This bag is near bag #14607 but not equal
//
// ------- description of functions ----------------
// Incorrect function
  function out_node1($k,$level)
  {
    global $name,$ind;
    for($i=0;$i<$level;$i++) echo "&nbsp;&nbsp;&nbsp;";
    echo "$name[$k]<BR>\n";
    foreach($ind as $i=>$index) {
      if($index==$k) out_node1($i,$level+1);
    }
  }
// Correct function (bypass bag)
  function out_node($k,$level)
  {
    global $name,$ind;
    for($i=0;$i<$level;$i++) echo "&nbsp;&nbsp;&nbsp;";
    echo "$name[$k]<BR>\n";
    foreach($ind as $i=>$index) {
      if($index==$k) $node[]=$i;
    }
    for($i=0;$i<count($node);$i++) out_node($node[$i],$level+1);
  }
//------------------------------------------------------------
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1251">
</head>
<body>
<?
// ------- tree structure ----------------
// Tree:            number   parrent index
//    node1            0       -1
//        leaf1        1        0
//        leaf2        2        0
//        node2        3        0
//            leaf3    4        3
//            leaf4    5        3
// ------- body of script ----------------
  $name=Array('node1','leaf1','leaf2','node2','leaf3','leaf4');
  $ind=Array('-1','0','0','0','3','3');
//------------------------------------------------------------
echo "<BR><B>Incorrect example</B><BR>\n";
// print tree
  out_node1(0,0);
echo "<BR><B>Correct example</B><BR>\n";
// print tree
  out_node(0,0);
?>
</body>
</html>


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


-- 
Edit this bug report at http://bugs.php.net/?id=22879&edit=1

Reply via email to