FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Weird behaviour when adding values to associative arrays
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Weird behaviour when adding values to associative arrays [message #177779] Fri, 20 April 2012 21:06 Go to next message
JF is currently offline  JF
Messages: 2
Registered: April 2012
Karma: 0
Junior Member
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.

Is it me? I shouldn't use that syntax? (found in a comment in php.net)

Thanks
Eugenio
Re: Weird behaviour when adding values to associative arrays [message #177781 is a reply to message #177779] Fri, 20 April 2012 21:23 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
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
Re: Weird behaviour when adding values to associative arrays [message #177782 is a reply to message #177781] Fri, 20 April 2012 21:43 Go to previous message
JF is currently offline  JF
Messages: 2
Registered: April 2012
Karma: 0
Junior Member
Il Fri, 20 Apr 2012 23:23:59 +0200, Michael Fesser ha scritto:

> 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.

Now everything's clear. Thank you!

Eugenio
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: We Need Speakers For MOSC2012
Next Topic: php include, function, ...
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 01:19:47 GMT 2024

Total time taken to generate the page: 0.02748 seconds