php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12193 nested loops using each don't work
Submitted: 2001-07-16 16:27 UTC Modified: 2001-07-16 16:50 UTC
From: adresse at brutele dot be Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.0.6 OS: windows
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: adresse at brutele dot be
New email:
PHP Version: OS:

 

 [2001-07-16 16:27 UTC] adresse at brutele dot be
when tho loops using each are nested, the outer while loop stops after the first iteration.

For instance, the output of the following script :

  $outer=array("o1", "o2");
  $inner=array("i1","i2");
  while (list($o_index, $o) = each($outer))
    while (list($i_index, $i) = each($inner))
      echo $o, $i, "<br>";
is
  o1i1
  o1i2
instead of
  o1i1
  o1i2
  o2i1
  o2i2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-16 16:50 UTC] zeev@php.net
list() = each() is not a loop.  each() is a simple function, that returns a key,value pair, and advances the internal array pointer to the next pair.  It has no idea it's being used inside a loop, and thus, nothing makes this internal pointer go back to the beginning of the array when the loop ends.

Solutions:
- Use foreach()
or
- Call reset() on the array at the end of the loop.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jan 18 07:01:30 2025 UTC