Re: Weird behaviour when adding values to associative arrays [message #177781 is a reply to message #177779] |
Fri, 20 April 2012 21:23 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma:
|
Senior Member |
|
|
.oO(JF)
> I made a small example to show this really strange behaviour.
> Basically, I have an array of 4 associative arrays declared like this:
>
> $houses[] = array(
> "metri" => "72",
> "costo" => "700",
> "note" => "Fourth item"
> );
>
> if, after declaring the arrays, I add a field to all records, like this:
>
> $count = 0;
> foreach ($houses as &$house) // note the ampersand
> $house['id'] = $count++;
>
> then one of the records is overwritten by another! Here is the weird
> output:
>
> http://www.geniorustico.eu/bug.php
>
> Three values are showed, while in the source four different values are
> listed. Here is the source:
>
> http://www.geniorustico.eu/bug.php.txt
>
> Finally, if I sort the array afterwards with usort and then print it, a
> different record is overwritten! Seems like it is totally messed up.
As a leftover from the first foreach loop $house is still a reference to
the last item of the array. If you then sort the array and run another
foreach loop with the same $house variable, which still references one
of the array items ... well, anything can happen.
> Is it me? I shouldn't use that syntax? (found in a comment in php.net)
The latter. Using foreach with references can be very tricky and should
be avoided. In most cases it's not really needed, because you can use
the "extended" version of foreach to also get the element's key, which
then allows to access it directly:
foreach ($houses as $key => $house) {
$houses[$key]['id'] = $count++;
}
Explicitly unsetting the $house variable after the first loop should
also fix the problem, but it's better to completely avoid it by not
using references in a foreach loop at all. It's a convenience feature,
but might have really nasty side effects.
HTH
Micha
--
http://mfesser.de/blickwinkel
|
|
|