php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #1115 each() should reset array pointers by default?
Submitted: 1999-02-02 17:57 UTC Modified: 2000-05-19 09:52 UTC
From: charlie at k4azl dot net Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 3.0.6 OS: Linux 2.0.35
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: charlie at k4azl dot net
New email:
PHP Version: OS:

 

 [1999-02-02 17:57 UTC] charlie at k4azl dot net
each() doesn't reset an array's internal pointer to the first element after executing, which can cause a lot of headaches if you expect that to be the case...  I don't know if this is intentional or an oversight, and I can imagine it being either one.

For instance:
<?
echo "<pre>firstpass\n\n";
while ( list( $k, $v ) = each( $GLOBALS ) ) { echo "\n$k => $v"; }
echo "\n\nsecondpass";
while ( list( $k, $v ) = each( $GLOBALS ) ) { echo "\n$k => $v"; }
?>

Will output this to the browser:

  firstpass

  GLOBALS => 
  HTTP_GET_VARS => Array
  HTTP_COOKIE_VARS => Array
  INIT_VERSION => sysvinit-2.69
  TERM => linux
  etc....

  secondpass
  <i>nothing at all...</i>

Of course this can be fixed by adding a call to reset($somearray) after each call to each($somearray), but that doesn't seem very intuitive.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-05-19 09:52 UTC] hholzgra at cvs dot php dot net
yeah, how should each() function distinuish betwenn
first and following cycle in while-loop
this ain't possible to implement in a sane way 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 00:01:36 2025 UTC