Req #50532 [Opn->Wfx]: Lazy-evaluated multiple-array foreach

From: Date: Fri, 15 Mar 2013 14:57:49 +0000
Subject: Req #50532 [Opn->Wfx]: Lazy-evaluated multiple-array foreach
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-177809@lists.php.net to get a copy of this message
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)

« previous php.bugs (#177809) next »