From:             
Operating system: 
PHP version:      5.2.17
Package:          *Programming Data Structures
Bug Type:         Bug
Bug description:Foreach not running through added elements when passed by 
reference

Description:
------------
I found out that foreach sometimes doesn't run through elements added to an
array 

when the value is passed by reference. I haven't tested enough to say it
for sure, 

but I think it happens only on elements added on what would be the last
iteration 

of foreach.

Test script:
---------------
$array = array('a', 'b', 'c');

foreach ($array as $i => &$val) {

   echo $val.' ';

   if ($i % 2 == 1) $array[] = $val.'2';

}

Expected result:
----------------
When it reaches $array[1] (b), it creates $array[3] with the value "b2".
Then it 

runs through $array[3] and creates $array[4] with the value "b22". After
that, it 

should run through $array[4] too, outputting altogether "a b c b2 b22" and


stopping.

Actual result:
--------------
Since at the start of the iteration, $array[3] is the last element of the
array, 

after $array[4] is created in that iteration, it doesn't run through it, 

outputting just "a b c b2". Still, $array[4] is created.

-- 
Edit bug report at http://bugs.php.net/bug.php?id=54321&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=54321&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=54321&r=trysnapshot53
Try a snapshot (trunk):              
http://bugs.php.net/fix.php?id=54321&r=trysnapshottrunk
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=54321&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=54321&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=54321&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=54321&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=54321&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=54321&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=54321&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=54321&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=54321&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=54321&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=54321&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=54321&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=54321&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=54321&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=54321&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=54321&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=54321&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=54321&r=mysqlcfg

Reply via email to