|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50532 Lazy-evaluated multiple-array foreach
Submitted: 2009-12-19 12:38 UTC Modified: 2013-03-15 14:57 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: jsimlo at yahoo dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: jsimlo at yahoo dot com
New email:
PHP Version: OS:


 [2009-12-19 12:38 UTC] jsimlo at yahoo dot com
I would like to suggest an extended foreach syntax like this:
foreach ($a, $b, $c as $k => $i)
  echo $i;
Which would result in the same execution as current:
foreach (array ($a, $b, $c) as $x)
  foreach ($x as $k => $i);
    echo $i;
The reason for requesting this is (simplicity and) possibility of lazy-evaluation of the source arrays. The source arrays can be expressions that take time to compute; or consume large memory each while they exist; and the body of the foreach can break the foreach cycle, rendering a lazy-evaluation quite worth the trouble. Example:
// several not-so-easy-to-compute arrays
foreach (
  as $child
   // searching for a child with a special name
   if ($child->GetName() == 'something') break;

// now we got a child of such name (or the last cycled child)
print_r ($child);

Reproduce code:
From manual page: control-structures.foreach


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-28 12:39 UTC]
-Package: Feature/Change Request +Package: Arrays related -Operating System: irrelevant +Operating System: * -PHP Version: 5.3.1 +PHP Version: *
 [2013-03-15 14:57 UTC]
-Status: Open +Status: Wont fix
 [2013-03-15 14:57 UTC]
I don't think it makes to extend the foreach syntax in that direction. It's hard to understand and rarely useful.

Instead use AppendIterator from SPL (
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 25 05:01:29 2024 UTC