php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42690 key function in foreach array does not start from the beginning of array
Submitted: 2007-09-17 23:13 UTC Modified: 2007-09-18 09:31 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: owidjaya at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.4 OS:
Private report: No CVE-ID: None
 [2007-09-17 23:13 UTC] owidjaya at gmail dot com
Description:
------------
key function within foreach array does not start from the very beginning of the array instead it starts from the 2nd element

Reproduce code:
---------------
$courses= array(                                                                                                                                             
               1=>"test",                                                                                                                                    
               2=>"test2",                                                                                                                                   
               3=>"test3"                                                                                                                                    
);                                                                                                                                                           
echo key($courses)."\n";                                                                                                                                     
foreach($courses as $key=>$course){                                                                                                                          
    echo $key ." ". $course."\n";                                                                                                                            
                                                                                                                                                             
}                                                                                                                                                            
reset($courses);                                                                                                                                             
echo "\n";                                                                                                                                                   
echo key($courses)."\n";                                                                                                                                     
foreach($courses as $course){                                                                                                                                
    echo key($courses) ." ". $course."\n";                                                                                                                   
    next($courses);                                                                                                                                          
                                                                                                                                                             
}     

Expected result:
----------------
1
1 test
2 test2
3 test3

1
1 test
2 test2
3 test3

Actual result:
--------------
1
1 test
2 test2
3 test3

1
2 test
3 test2
 test3


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-18 09:31 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Using key/next/prev/etc. in foreach() is unsupported. (foreach does an internal reset() in it too!) 

And using key() in foreach is pretty useless since it provides that with syntax foreach($arr as $key => $val)
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Aug 01 15:01:24 2021 UTC