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
Return to the default flat view Create a new topic Submit Reply
Weird behaviour when adding values to associative arrays [message #177779] Fri, 20 April 2012 21:06 Go to previous message
JF is currently offline  JF
Messages: 2
Registered: April 2012
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: We Need Speakers For MOSC2012
Next Topic: php include, function, ...
Goto Forum:
  

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

Current Time: Thu Dec 12 11:44:05 GMT 2024

Total time taken to generate the page: 0.04085 seconds