# New syntax for multidimensional array loop with foreach

- Thread: https://externals.io/message/67945
- Original archive: https://news-web.php.net/php.internals/67945
- Started by Christian Stoller on 2013-06-27T14:10:17+00:00
- Messages: 15
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #67945 — Christian Stoller — 2013-06-27T14:10:17+00:00

URL: https://externals.io/message/67945#67945

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

---

## #67946 — Florin Patan — 2013-06-27T14:18:33+00:00

URL: https://externals.io/message/67945#67946 · In reply to: #67945

> 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) { ... }


Best regards
----
Florin Patan
https://github.com/dlsniper
http://www.linkedin.com/in/florinpatan

---

## #67947 — Kingsquare.nl - Robin Speekenbrink — 2013-06-27T14:22:51+00:00

URL: https://externals.io/message/67945#67947 · In reply to: #67946

I'd thought this would have been `solved` by allowing the list statement in
foreach (
http://nl3.php.net/manual/en/control-structures.foreach.php#control-structures.foreach.list
)

Doesnt that solve your problem already?

Kind regards,

Robin Speekenbrink


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

> On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller <stoller@leonex.de>
> wrote:
> > […]
> foreach. I haven’t seen such a syntax until now, but I think it is easy to
> understand ;-)
> > […]
> 2D (or more) array:
> > […]
> it's a bit too confusing?
> > […]
>
>
> 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) { ... }
>
>
> Best regards
> ----
> Florin Patan
> https://github.com/dlsniper
> http://www.linkedin.com/in/florinpatan

---

## #67948 — Sebastian Krebs — 2013-06-27T14:24:54+00:00

URL: https://externals.io/message/67945#67948 · In reply to: #67947

2013/6/27 Kingsquare.nl - Robin Speekenbrink <robin@kingsquare.nl>

> I'd thought this would have been `solved` by allowing the list statement in
> foreach (
>
> http://nl3.php.net/manual/en/control-structures.foreach.php#control-structures.foreach.list
> )
>
> Doesnt that solve your problem already?
>

This is only useable for structs, not for lists, or sets.


>
> Kind regards,
>
> Robin Speekenbrink
>
>
> 2013/6/27 Florin Patan <florinpatan@gmail.com>
>
> > […]
> to
> > […]
>



-- 
github.com/KingCrunch

---

## #67949 — Sebastian Krebs — 2013-06-27T14:27:05+00:00

URL: https://externals.io/message/67945#67949 · In reply to: #67946

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

> On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller <stoller@leonex.de>
> wrote:
> > […]
> foreach. I haven’t seen such a syntax until now, but I think it is easy to
> understand ;-)
> > […]
> 2D (or more) array:
> > […]
> it's a bit too confusing?
> > […]
>
>
> 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
>
> --
> 
> 
>
>


-- 
github.com/KingCrunch

---

## #67950 — Ralf Lang — 2013-06-27T14:27:52+00:00

URL: https://externals.io/message/67945#67950 · In reply to: #67945

> 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?


-- 
Ralf Lang
Linux Consultant / Developer
Tel.: +49-170-6381563
Mail: lang@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

---

## #67953 — Yahav Gindi Bar — 2013-06-27T14:54:04+00:00

URL: https://externals.io/message/67945#67953 · In reply to: #67950

> 
> > […]
> foreach. I haven’t seen such a syntax until now, but I think it is easy to
> understand ;-)
> > […]
> 2D (or more) array:
> > […]
> it's a bit too confusing?
> > […]
>
> 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: lang@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

---

## #67955 — Nikita Popov — 2013-06-27T14:58:13+00:00

URL: https://externals.io/message/67945#67955 · In reply to: #67945

On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller <stoller@leonex.de>wrote:

> 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
> }
>

I'm against this feature. It makes the code marginally shorter, but a good
bit less readable. Combined with the break/continue issue, I don't think we
gain anything by introducing this syntax.

Nikita

---

## #67966 — johannes@schlueters.de — 2013-06-27T16:14:17+00:00

URL: https://externals.io/message/67945#67966 · In reply to: #67955

> On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller <stoller@leonex.de>wrote:
> 
> > […]
> 
> I'm against this feature. It makes the code marginally shorter, but a good
> bit less readable. Combined with the break/continue issue, I don't think we
> gain anything by introducing this syntax.

+1

Let me add to this:

While the pattern pattern exists often there are things to be done in
the outer iteration, too. So I think the pure form ofthis double-foreach
is not as common as it might seem ...

And you can write 
   foreach ($array as $i) foreach ($i as $innerKey => $value) {
   }
it is not that much longer, but way more explicit on what is going on,
visually parsing the suggested form needs more concentration.

johannes

---

## #67977 — Patrick ALLAERT — 2013-06-27T17:09:46+00:00

URL: https://externals.io/message/67945#67977 · In reply to: #67966

2013/6/27 Johannes Schlüter <johannes@schlueters.de>:
> 
> > […]
>
> +1
>
> Let me add to this:
>
> While the pattern pattern exists often there are things to be done in
> the outer iteration, too. So I think the pure form ofthis double-foreach
> is not as common as it might seem ...
>
> And you can write
>    foreach ($array as $i) foreach ($i as $innerKey => $value) {
>    }
> it is not that much longer, but way more explicit on what is going on,
> visually parsing the suggested form needs more concentration.
>
> johannes

+1 to what Nikita and Johannes said.

---

## #67978 — Pierre du Plessis — 2013-06-27T17:13:43+00:00

URL: https://externals.io/message/67945#67978 · In reply to: #67966

On Thu, Jun 27, 2013 at 6:14 PM, Johannes Schlüter
<johannes@schlueters.de>wrote:

> 
> > […]
> it's
> > […]
>

With the addition of array_column in php 5.5, this can be done in a much
cleaner way:

$array = array(
    array('value' => 1),
    array('value' => 2),
    array('value' => 3),
);

$count = 0;

foreach(array_column($array, 'value') as $value) {
 $count += $value;
}

---

## #67980 — Tjerk Anne Meesters — 2013-06-27T17:29:52+00:00

URL: https://externals.io/message/67945#67980 · In reply to: #67978

On Fri, Jun 28, 2013 at 1:13 AM, Pierre du Plessis
<pierre@pcservice.co.za>wrote:

> On Thu, Jun 27, 2013 at 6:14 PM, Johannes Schlüter
> <johannes@schlueters.de>wrote:
>
> > […]
>
> With the addition of array_column in php 5.5, this can be done in a much
> cleaner way:
>
> $array = array(
>     array('value' => 1),
>     array('value' => 2),
>     array('value' => 3),
> );
>
> $count = 0;
>
> foreach(array_column($array, 'value') as $value) {
>  $count += $value;
> }
>

And what about the "// and do something with $key and $innerKey" part?



-- 
--
Tjerk

---

## #67982 — Pierre du Plessis — 2013-06-27T17:36:30+00:00

URL: https://externals.io/message/67945#67982 · In reply to: #67980

On Thu, Jun 27, 2013 at 7:29 PM, Tjerk Anne Meesters <datibbaw@php.net>wrote:

> On Fri, Jun 28, 2013 at 1:13 AM, Pierre du Plessis
> <pierre@pcservice.co.za>wrote:
>
> > […]
> stoller@leonex.de
> > […]
> maybe
> > […]
>
> And what about the "// and do something with $key and $innerKey" part?


This is using the original example provided, which didn't do anything with
the $key and $innerKey.

---

## #67983 — Tjerk Anne Meesters — 2013-06-27T17:57:58+00:00

URL: https://externals.io/message/67945#67983 · In reply to: #67982

On Fri, Jun 28, 2013 at 1:36 AM, Pierre du Plessis
<pierre@pcservice.co.za>wrote:

> On Thu, Jun 27, 2013 at 7:29 PM, Tjerk Anne Meesters <datibbaw@php.net>wrote:
>
> > […]
>
>
> This is using the original example provided, which didn't do anything with
> the $key and $innerKey.
>

It's good to see how you have developed a keen sense of code optimization;
however, that's not doing much good here, because the intention of the
shown code is that those variables are actually used, which is what the
commented line conveys :)



-- 
--
Tjerk

---

## #68003 — Benjamin Eberlei — 2013-06-28T09:21:29+00:00

URL: https://externals.io/message/67945#68003 · In reply to: #67945

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
