note 73218 added to control-structures.foreach

From: Date: Tue, 13 Feb 2007 19:56:32 +0000
Subject: note 73218 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-123330@lists.php.net to get a copy of this message
Hi everyone. I've run into an interesting issue with foreach and I was wondering if anybody has experienced anything similar or if anybody can suggest what may be causing the problem. I have built up an array with the following structure: Array ( [1000128] => Array ( [month] => Array ( [signups] => 1 [conversionRatio] => 100 ) [day] => Array ( [conversionRatio] => 0 ) [week] => Array ( [conversionRatio] => 0 ) [name] => User 1 (1000128) ) [1000132] => Array ( [month] => Array ( [signups] => 1 [conversionRatio] => 100 ) [day] => Array ( [conversionRatio] => 0 ) [week] => Array ( [conversionRatio] => 0 ) [name] => User 2 (1000132) ) [1000142] => Array ( [month] => Array ( [signups] => 2 [conversionRatio] => 100 ) [day] => Array ( [conversionRatio] => 0 ) [week] => Array ( [conversionRatio] => 0 ) [name] => User 3 (1000142) ) ) I then use uasort() to sort the array and array_reverse() to put the sort in descending order. Finally, I use foreach to iterate through this array to display the results in a simple HTML table. The problem is that the iteration is duplicating one of the records seemingly at random. For instance, instead of seeing: User 3: signups=2 conversionRatio=100% User 1: signups=1 conversionRatio=100% User 2: signups=1 conversionRatio=100% as expected, I get User 3: signups=2 conversionRatio=100% User 1: signups=1 conversionRatio=100% User 1: signups=1 conversionRatio=100% When I print the array using print_r just prior to the foreach loop, everything seems to be in order. At first, I thought there might be some issue with the internal pointer so I've tried using reset(), and also tried removing the calls to uasort() and array_reverse(). The same problem appears, just with different records being duplicated each time. I've checked the value of $id within the foreach (I use the line foreach ($arr as $id => $values)) and each id is being printed in turn but the $value portion (in this case corresponding to 1000142) appears to be overwritten with the previous $value array. As a temporary workaround, I've gone to using foreach (array_keys($arr) as $id) and then pulling individual values by using $arr[$id][...] and this is working as expected. However, I would obviously like to avoid this hack as I do use foreach quite extensively in other scripts and the appearance of this problem is quite unsettling. Any feedback will be greatly appreciated. Thanks! ---- Server IP: 216.235.15.211 Probable Submitter: 66.119.167.122 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/73218 Del: integrated -- https://master.php.net/note/delete/73218/integrated Del: useless -- https://master.php.net/note/delete/73218/useless Del: bad code -- https://master.php.net/note/delete/73218/bad+code Del: spam -- https://master.php.net/note/delete/73218/spam Del: non-english -- https://master.php.net/note/delete/73218/non-english Del: in docs -- https://master.php.net/note/delete/73218/in+docs Del: other reasons-- https://master.php.net/note/delete/73218 Reject -- https://master.php.net/note/reject/73218 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123330) next »