Req #50532 [Opn->Wfx]: Lazy-evaluated multiple-array foreach
Edit report at https://bugs.php.net/bug.php?id=50532&edit=1
ID: 50532
Updated by: nikic@php.net
Reported by: jsimlo at yahoo dot com
Summary: Lazy-evaluated multiple-array foreach
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Arrays related
Operating System: *
PHP Version: *
Block user comment: N
Private report: N
New Comment:
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 (http://php.net/appenditerator).
Previous Comments:
------------------------------------------------------------------------
[2009-12-19 12:38:04] jsimlo at yahoo dot com
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
---
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=50532&edit=1
Thread (2 messages)