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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: owidjaya at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC