2013/6/27 Florin Patan <florinpa...@gmail.com>

> On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller <stol...@leonex.de>
> 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
> > }
> >
> > If the keys aren't needed, you can shorten it to:
> >
> > $count = 0;
> > foreach ($array as $innerArray as $value) {
> >     $count += $value;
> > }
> >
> > What do you think?
> >
> > --
> > Christian Stoller
> > LEONEX Internet GmbH
>
>
> Hi,
>
>
> Quick question, how would the engine then treat this case:
>
> $array = array();
> $array['level1.1']['level2']['level3'] = 'value';
> $array['level1.2'] = new StdClass();
>
> foreach($array as $level1 as $level2 as $level3) { ... }
>

When I get this right it would fail. As far as I see this is equivalent to

foreach ($array as $level1) {
    foreach ($level1 as $level2) {
        foreach ($level2 as $level3) {
            // ..
        }
    }
}


So I'd say they should behave the same.


I for myself find this proposal at least interesting. I don't know how many
foreach-chains I see every day :(


Regards,
Sebastian


>
>
> Best regards
> ----
> Florin Patan
> https://github.com/dlsniper
> http://www.linkedin.com/in/florinpatan
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
github.com/KingCrunch

Reply via email to