Okay, you're right that it starts working fine when I rename the $item to
$blah, but your explanation doesn't make much sense.

After the first foreach, $item points to the last one, that I already
figured. If you ask me, the second foreach should replace the instance of
$item, not overwrite it. But it doesn't seem to overwrite it, because if you
look at the results, the last element is replaced by the contents of the one
before the last element. If you do these loops with $i < 10, you will see it
more clearly maybe.

The last item in the array is replaced, not by the first one of the second
foreach, but by the item before the last item. That just doesn't make sense
at all.

And really, I do consider foreach() overwriting previous references a bug.
If it wasn't, then this:

$i=0;
foreach ($array as &$foo)
  $foo = $i++;

should have the last value on every item, because within this foreach() the
$foo's should overwrite eachother with your logic.

Ron


"Antony Dovgal" <[EMAIL PROTECTED]> schreef in bericht
news:[EMAIL PROTECTED]
> On 16.09.2005 16:29, Ron Korving wrote:
> > My apologies for my DOM-mistake today, but right now I came across
something
> > that totally stunned me. It's a total paradox situation that just has to
be
> > one of the biggest bugs I've ever come across in PHP. Personally, I
think
> > it's rather high-priority because it's an engine problem, and not an
issue
> > with some extension.
> >
> > Here's the code:
> >
> > <?php
> >   $array = array();
> >
> >   for ($i=0; $i < 3; $i++)
> >     $array[] = array("foo" => $i);
> >
> >   foreach ($array as &$item)
> >     $item["bar"] = $item["foo"] * 2;
> >
> >   print_r($array);
> >
> >   foreach ($array as $item)
> >     print_r($item);
> > ?>
>
> Ron, after the first foreach $item still points to the last element of
$array (because you've used &$item syntax).
> The second foreach changes $item, but it *STILL* references the last
element of $array, so it actually changes it instead.
>
> Try to change this part of the code:
>    foreach ($array as $item)
>      print_r($item);
>
> to:
>    foreach ($array as $blah)
>      print_r($blah);
>
> you'll see it yourself.
>
> I don't see any bugs here.
>
> -- 
> Wbr,
> Antony Dovgal

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to