Re: New syntax for multidimensional array loop with foreach
| From: | Benjamin Eberlei | Date: | Fri, 28 Jun 2013 09:21:29 +0000 |
| Subject: | Re: New syntax for multidimensional array loop with foreach | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-68003@lists.php.net to get a copy of this message | ||
You can build an iterator to do this for you, doesn't need to be in the
language IMHO
On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller <stoller@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
>