[PHP-DEV] Bug #14607 Updated: Nested foreach (on the same array variable) don't work as should

2001-12-20 Thread TheWizardRK

ID: 14607
User updated by: [EMAIL PROTECTED]
Reported By: [EMAIL PROTECTED]
Old Status: Feedback
Status: Open
Bug Type: Scripting Engine problem
Operating System: Windows 98
PHP Version: 4.0.6
New Comment:

$tree = Array(
  [1] = Array ( [parentid] = 0, [name] = '1' )
  [2] = Array ( [parentid] = 0, [name] = '2' )
  [3] = Array ( [parentid] = 1, [name] = '1_1' )
  [4] = Array ( [parentid] = 3, [name] = '1_1_1' )
  [5] = Array ( [parentid] = 3, [name] = '1_1_2' )
  [6] = Array ( [parentid] = 4, [name] = '1_1_1_1' )
  [7] = Array ( [parentid] = 1, [name] = '1_2' )
  [8] = Array ( [parentid] = 7, [name] = '1_2_1' )
  [9] = Array ( [parentid] = 2, [name] = '2_1' )
);

// Recursion using a copy of the variable each time
// WORKS
function funktzia1($base,$dir)
{
  foreach($dir as $id = $item)
  {
if ($item[parentid]==$base)
{
  echo
UL\nLI.$item[name]./LIBR\n;
  funktzia1($id,$dir);
  echo /UL\n;
}
  }
}

// Print the tree using the first function
funktzia1(0,$tree);


// Recursion using the same variable - pass by reference (note the  before the 
$dir)
// DOESN'T WORK
function funktzia2($base,$dir)
{
  foreach($dir as $id = $item)
  {
if ($item[parentid]==$base)
{
  echo
UL\nLI.$item[name]./LIBR\n;
  funktzia2($id,$dir);
  echo /UL\n;
}
  }
}

// Print the tree using the second function
funktzia2(0,$tree);

Previous Comments:


[2001-12-19 18:31:46] [EMAIL PROTECTED]

Please provide a compelte self-contained copypaste ready script.

Feedback.



[2001-12-19 15:17:21] [EMAIL PROTECTED]

1. Actually doing a reset() doesn't work at all.

2. I thought that this was a duplicate of #5052 - but now I see that there's too 
different variables there for each level of the nested foreach() - and I use the 
same variable.
So it seems that this is not a duplicate after all (corrent me if I'm wrong).



[2001-12-19 15:00:55] [EMAIL PROTECTED]

It seems that this has been pointed out before... Sorry.
But this is still not solved (although the other bug reports about this are already 
marked as closed). :(
I only saw a solution of using reset() in the end of the loop, but this doesn't seem 
to be a good solution.
Any chances that each foreach() will be made to work on a new copy of the array, so 
that nested foreach() will work?



[2001-12-19 14:46:50] [EMAIL PROTECTED]

I have a tree, which is loaded into a variable into the following (hopefully 
self-explanatory) structure:

Array(
  [1] = Array ( [parentid] = 0, [name] = 1 )
  [2] = Array ( [parentid] = 0, [name] = 2 )
  [3] = Array ( [parentid] = 1, [name] = 1_1 )
  [4] = Array ( [parentid] = 3, [name] = 1_1_1 )
  [5] = Array ( [parentid] = 3, [name] = 1_1_2 )
  [6] = Array ( [parentid] = 4, [name] = 1_1_1_1 )
  [7] = Array ( [parentid] = 1, [name] = 1_2 )
  [8] = Array ( [parentid] = 7, [name] = 1_2_1 )
  [9] = Array ( [parentid] = 2, [name] = 2_1 )
) 

This results in the following three:
+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1
  + 1_1_2
 + 1_2
  + 1_2_1
+ 2
 + 2_1

I have the following recursive function to draw this tree:

function funktzia($base,$dir)
{
  foreach($dir as $id = $item)
  {
if ($item[parentid]==$base)
{
  echo UL\nLI.$item[name]./LIBR\n;
  funktzia($id,$dir);
  echo /UL\n;
}
  }
}

where `$base' is the current tree node id, and `$dir' is the variable which holds the 
tree, as described above.
The function is initially called funktzia(0,$tree);, and the recursively calls 
itself.

So far so good.
But if the `$dir' variable is a by-reference parameter (i.e. [...] $dir [...] 
instead of [...] $dir [...] as now), or it is defined global within the function 
instead of being passed as a parameter (in either way, all recursion level use the 
same variable) - it doesn't work.
It draw the tree only while going up levels, but stop when it's time to go back to a 
lower level. Or in short, the following tree is drawn instead of the full one:

+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1

It seems that when we go back to the lower foreach loop, the array pointer it uses 
already points to the end of the array (since we just finished doing a foreach loop 
on that array in the higher level).





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


-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP-DEV] Bug #14607: Nested foreach (on the same array variable) don't work as should

2001-12-19 Thread TheWizardRK

From: [EMAIL PROTECTED]
Operating system: Windows 98
PHP version:  4.0.6
PHP Bug Type: Scripting Engine problem
Bug description:  Nested foreach (on the same array variable) don't work as should

I have a tree, which is loaded into a variable into the following
(hopefully self-explanatory) structure:

Array(
  [1] = Array ( [parentid] = 0, [name] = 1 )
  [2] = Array ( [parentid] = 0, [name] = 2 )
  [3] = Array ( [parentid] = 1, [name] = 1_1 )
  [4] = Array ( [parentid] = 3, [name] = 1_1_1 )
  [5] = Array ( [parentid] = 3, [name] = 1_1_2 )
  [6] = Array ( [parentid] = 4, [name] = 1_1_1_1 )
  [7] = Array ( [parentid] = 1, [name] = 1_2 )
  [8] = Array ( [parentid] = 7, [name] = 1_2_1 )
  [9] = Array ( [parentid] = 2, [name] = 2_1 )
) 

This results in the following three:
+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1
  + 1_1_2
 + 1_2
  + 1_2_1
+ 2
 + 2_1

I have the following recursive function to draw this tree:

function funktzia($base,$dir)
{
  foreach($dir as $id = $item)
  {
if ($item[parentid]==$base)
{
  echo UL\nLI.$item[name]./LIBR\n;
  funktzia($id,$dir);
  echo /UL\n;
}
  }
}

where `$base' is the current tree node id, and `$dir' is the variable which
holds the tree, as described above.
The function is initially called funktzia(0,$tree);, and the recursively
calls itself.

So far so good.
But if the `$dir' variable is a by-reference parameter (i.e. [...] $dir
[...] instead of [...] $dir [...] as now), or it is defined global
within the function instead of being passed as a parameter (in either way,
all recursion level use the same variable) - it doesn't work.
It draw the tree only while going up levels, but stop when it's time to go
back to a lower level. Or in short, the following tree is drawn instead of
the full one:

+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1

It seems that when we go back to the lower foreach loop, the array
pointer it uses already points to the end of the array (since we just
finished doing a foreach loop on that array in the higher level).
-- 
Edit bug report at: http://bugs.php.net/?id=14607edit=1


-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP-DEV] Bug #14607 Updated: Nested foreach (on the same array variable) don't work as should

2001-12-19 Thread TheWizardRK

ID: 14607
User updated by: [EMAIL PROTECTED]
Reported By: [EMAIL PROTECTED]
Old Status: Open
Status: Duplicate
Bug Type: Scripting Engine problem
Operating System: Windows 98
PHP Version: 4.0.6
New Comment:

It seems that this has been pointed out before... Sorry.
But this is still not solved (although the other bug reports about this are already 
marked as closed). :(
I only saw a solution of using reset() in the end of the loop, but this doesn't seem 
to be a good solution.
Any chances that each foreach() will be made to work on a new copy of the array, so 
that nested foreach() will work?

Previous Comments:


[2001-12-19 14:46:50] [EMAIL PROTECTED]

I have a tree, which is loaded into a variable into the following (hopefully 
self-explanatory) structure:

Array(
  [1] = Array ( [parentid] = 0, [name] = 1 )
  [2] = Array ( [parentid] = 0, [name] = 2 )
  [3] = Array ( [parentid] = 1, [name] = 1_1 )
  [4] = Array ( [parentid] = 3, [name] = 1_1_1 )
  [5] = Array ( [parentid] = 3, [name] = 1_1_2 )
  [6] = Array ( [parentid] = 4, [name] = 1_1_1_1 )
  [7] = Array ( [parentid] = 1, [name] = 1_2 )
  [8] = Array ( [parentid] = 7, [name] = 1_2_1 )
  [9] = Array ( [parentid] = 2, [name] = 2_1 )
) 

This results in the following three:
+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1
  + 1_1_2
 + 1_2
  + 1_2_1
+ 2
 + 2_1

I have the following recursive function to draw this tree:

function funktzia($base,$dir)
{
  foreach($dir as $id = $item)
  {
if ($item[parentid]==$base)
{
  echo UL\nLI.$item[name]./LIBR\n;
  funktzia($id,$dir);
  echo /UL\n;
}
  }
}

where `$base' is the current tree node id, and `$dir' is the variable which holds the 
tree, as described above.
The function is initially called funktzia(0,$tree);, and the recursively calls 
itself.

So far so good.
But if the `$dir' variable is a by-reference parameter (i.e. [...] $dir [...] 
instead of [...] $dir [...] as now), or it is defined global within the function 
instead of being passed as a parameter (in either way, all recursion level use the 
same variable) - it doesn't work.
It draw the tree only while going up levels, but stop when it's time to go back to a 
lower level. Or in short, the following tree is drawn instead of the full one:

+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1

It seems that when we go back to the lower foreach loop, the array pointer it uses 
already points to the end of the array (since we just finished doing a foreach loop 
on that array in the higher level).





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


-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP-DEV] Bug #14607 Updated: Recursive nested foreach doesn't work as should

2001-12-19 Thread TheWizardRK

ID: 14607
User updated by: [EMAIL PROTECTED]
Reported By: [EMAIL PROTECTED]
Old Summary: Nested foreach (on the same array variable) don't work as should
Old Status: Duplicate
Status: Open
Bug Type: Scripting Engine problem
Operating System: Windows 98
PHP Version: 4.0.6
New Comment:

1. Actually doing a reset() doesn't work at all.

2. I thought that this was a duplicate of #5052 - but now I see that there's too 
different variables there for each level of the nested foreach() - and I use the 
same variable.
So it seems that this is not a duplicate after all (corrent me if I'm wrong).

Previous Comments:


[2001-12-19 15:00:55] [EMAIL PROTECTED]

It seems that this has been pointed out before... Sorry.
But this is still not solved (although the other bug reports about this are already 
marked as closed). :(
I only saw a solution of using reset() in the end of the loop, but this doesn't seem 
to be a good solution.
Any chances that each foreach() will be made to work on a new copy of the array, so 
that nested foreach() will work?



[2001-12-19 14:46:50] [EMAIL PROTECTED]

I have a tree, which is loaded into a variable into the following (hopefully 
self-explanatory) structure:

Array(
  [1] = Array ( [parentid] = 0, [name] = 1 )
  [2] = Array ( [parentid] = 0, [name] = 2 )
  [3] = Array ( [parentid] = 1, [name] = 1_1 )
  [4] = Array ( [parentid] = 3, [name] = 1_1_1 )
  [5] = Array ( [parentid] = 3, [name] = 1_1_2 )
  [6] = Array ( [parentid] = 4, [name] = 1_1_1_1 )
  [7] = Array ( [parentid] = 1, [name] = 1_2 )
  [8] = Array ( [parentid] = 7, [name] = 1_2_1 )
  [9] = Array ( [parentid] = 2, [name] = 2_1 )
) 

This results in the following three:
+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1
  + 1_1_2
 + 1_2
  + 1_2_1
+ 2
 + 2_1

I have the following recursive function to draw this tree:

function funktzia($base,$dir)
{
  foreach($dir as $id = $item)
  {
if ($item[parentid]==$base)
{
  echo UL\nLI.$item[name]./LIBR\n;
  funktzia($id,$dir);
  echo /UL\n;
}
  }
}

where `$base' is the current tree node id, and `$dir' is the variable which holds the 
tree, as described above.
The function is initially called funktzia(0,$tree);, and the recursively calls 
itself.

So far so good.
But if the `$dir' variable is a by-reference parameter (i.e. [...] $dir [...] 
instead of [...] $dir [...] as now), or it is defined global within the function 
instead of being passed as a parameter (in either way, all recursion level use the 
same variable) - it doesn't work.
It draw the tree only while going up levels, but stop when it's time to go back to a 
lower level. Or in short, the following tree is drawn instead of the full one:

+ 1
 + 1_1
  + 1_1_1
   + 1_1_1_1

It seems that when we go back to the lower foreach loop, the array pointer it uses 
already points to the end of the array (since we just finished doing a foreach loop 
on that array in the higher level).





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


-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]