Re: New syntax for multidimensional array loop with foreach
| From: | Tjerk Anne Meesters | Date: | Thu, 27 Jun 2013 17:57:58 +0000 |
| Subject: | Re: New syntax for multidimensional array loop with foreach | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-67983@lists.php.net to get a copy of this message | ||
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:
>
>> 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:
>> >
>> > > 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
>> > > > > }
>> > > > >
>> > >
>> >
>> > 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