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 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
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: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 GmbHHi,
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
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
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 GmbHHi,
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--
2013/6/27 Florin Patan florinpatan@gmail.com
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 GmbHHi,
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--
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
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: 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
On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller stoller@leonex.dewrote:
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
On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller stoller@leonex.dewrote:
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
2013/6/27 Johannes Schlüter johannes@schlueters.de:
On Thu, Jun 27, 2013 at 4:10 PM, Christian Stoller stoller@leonex.dewrote:
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.
On Thu, Jun 27, 2013 at 6:14 PM, Johannes Schlüter
johannes@schlueters.dewrote:
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
}
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;
}
On Fri, Jun 28, 2013 at 1:13 AM, Pierre du Plessis
pierre@pcservice.co.zawrote:
On Thu, Jun 27, 2013 at 6:14 PM, Johannes Schlüter
johannes@schlueters.dewrote: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
}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
On Thu, Jun 27, 2013 at 7:29 PM, Tjerk Anne Meesters datibbaw@php.netwrote:
On Fri, Jun 28, 2013 at 1:13 AM, Pierre du Plessis
pierre@pcservice.co.zawrote:On Thu, Jun 27, 2013 at 6:14 PM, Johannes Schlüter
johannes@schlueters.dewrote: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
}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?
This is using the original example provided, which didn't do anything with
the $key and $innerKey.
On Fri, Jun 28, 2013 at 1:36 AM, Pierre du Plessis
pierre@pcservice.co.zawrote:
On Thu, Jun 27, 2013 at 7:29 PM, Tjerk Anne Meesters datibbaw@php.netwrote:
On Fri, Jun 28, 2013 at 1:13 AM, Pierre du Plessis
pierre@pcservice.co.zawrote:On Thu, Jun 27, 2013 at 6:14 PM, Johannes Schlüter
johannes@schlueters.dewrote: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
}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?
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
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.dewrote:
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