On Thu, Jun 27, 2013 at 5:27 PM, Ralf Lang <l...@b1-systems.de> wrote:

> On 27.06.2013 16:10, Christian Stoller wrote:
> > Hi internals,
> >
> > during my current work I had an idea for shorter array iteration with
> foreach. I haven’t seen such a syntax until now, but I think it is easy to
> understand ;-)
> >
> > Maybe you know the case where you have to iterate over all values of a
> 2D (or more) array:
> >
> > $count = 0;
> > foreach ($array as $key => $innerArray) {
> >     foreach ($innerArray as $innerKey => $value) {
> >         $count += $value;
> >         // and do something with $key and $innerKey
> >     }
> > }
> >
> > The new syntax could make it shorter and faster to write... but maybe
> it's a bit too confusing?
> >
> > $count = 0;
> > foreach ($array as $key => $innerArray as $innerKey => $value) {
> >     $count += $value;
> >     // and do something with $key and $innerKey
> > }
> > What do you think?
>
> How would a break or continue on this behave? Would it end the inner or
> the outer circle?
>
> I guess that the usage of continue/break with specific number is the best
in this case...
foreach ($array as $key => $innerArray
                            as $innerKey => $value) {
      $count += $value;
      if ($count % 2 == 0) { // for instance...
              break 2; // End loop
     } else if ( $count % 3 == 0 ) {
               break; // End inner loop
    } else if ( $count % 4 == 0) {
              continue 2; // Continue outer loop
   }

   continue; // Continue inner loop
}

Personally, this syntax (the multi-dims loop) seems unreadable...


>
> --
> Ralf Lang
> Linux Consultant / Developer
> Tel.: +49-170-6381563
> Mail: l...@b1-systems.de
> B1 Systems GmbH
> Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
> GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537
>
>

Reply via email to