|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-12-28 12:39 UTC] jani@php.net
-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] nikic@php.net
-Status: Open
+Status: Wont fix
[2013-03-15 14:57 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 02:00:01 2025 UTC |
Description: ------------ I would like to suggest an extended foreach syntax like this: <?php foreach ($a, $b, $c as $k => $i) echo $i; ?> Which would result in the same execution as current: <?php 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: <?php // several not-so-easy-to-compute arrays foreach ( $item1->GetChilds(), $item2->GetChilds(), $item3->GetParent()->GetChilds(), $item3->GetComputeSomethingDifficultChilds() 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 ---