Re: New syntax for multidimensional array loop with foreach

From: Date: Thu, 27 Jun 2013 17:09:46 +0000
Subject: Re: New syntax for multidimensional array loop with foreach
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-67977@lists.php.net to get a copy of this message
2013/6/27 Johannes Schlüter <johannes@schlueters.de>: > On Thu, 2013-06-27 at 16:58 +0200, Nikita Popov wrote: >> 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. > > +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.

« previous php.internals (#67977) next »