# New feature: additional syntax proposal for Foreach

- Thread: https://externals.io/message/68058
- Original archive: https://news-web.php.net/php.internals/68058
- Started by Chris London on 2013-07-03T20:01:38+00:00
- Messages: 3
- 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

---

## #68058 — Chris London — 2013-07-03T20:01:38+00:00

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

Hello community,

This is my first post here. I hope to improve my participation in the
community and give back to the technology that has done so much for my
career.  My first attempt is to propose an additional syntax for the
Foreach statement.

Background:

There are times in our code where we can leave things blank that we don't
need, such as:

for ( ; ; ) {

}

- and -

list( , $foo, $bar) = array('ignore', 'myFoo', 'myBar');

Proposal:

I have found over the years that there are times when I want to loop
through an associative array but I don't need the value. I would like to
allow the following syntax:

foreach ($array as $key => ) {

}

Please let me know your thoughts.

Thanks
Chris London

---

## #68059 — Kingsquare.nl - Robin Speekenbrink — 2013-07-03T20:07:11+00:00

URL: https://externals.io/message/68058#68059 · In reply to: #68058

Welcome Chris,

As a `plain` user of PHP, i'd like to advocate not to allow anymore
aliasing of ways to do stuff...
Your example should just read foreach(array_keys($array) as $key) {} That's
the only way to convey the actual intent of working with keys and
explicitly leaving out the values ;)

But hey, just my 2c...

Met vriendelijke groet,

Robin Speekenbrink
Kingsquare BV


2013/7/3 Chris London <me@chrislondon.co>

> Hello community,
>
> This is my first post here. I hope to improve my participation in the
> community and give back to the technology that has done so much for my
> career.  My first attempt is to propose an additional syntax for the
> Foreach statement.
>
> Background:
>
> There are times in our code where we can leave things blank that we don't
> need, such as:
>
> for ( ; ; ) {
>
> }
>
> - and -
>
> list( , $foo, $bar) = array('ignore', 'myFoo', 'myBar');
>
> Proposal:
>
> I have found over the years that there are times when I want to loop
> through an associative array but I don't need the value. I would like to
> allow the following syntax:
>
> foreach ($array as $key => ) {
>
> }
>
> Please let me know your thoughts.
>
> Thanks
> Chris London

---

## #68060 — Nikita Popov — 2013-07-03T21:01:39+00:00

URL: https://externals.io/message/68058#68060 · In reply to: #68058

> Hello community,
>
> This is my first post here. I hope to improve my participation in the
> community and give back to the technology that has done so much for my
> career.  My first attempt is to propose an additional syntax for the
> Foreach statement.
>
> Background:
>
> There are times in our code where we can leave things blank that we don't
> need, such as:
>
> for ( ; ; ) {
>
> }
>
> - and -
>
> list( , $foo, $bar) = array('ignore', 'myFoo', 'myBar');
>
> Proposal:
>
> I have found over the years that there are times when I want to loop
> through an associative array but I don't need the value. I would like to
> allow the following syntax:
>
> foreach ($array as $key => ) {
>
> }
>
> Please let me know your thoughts.
>
> Thanks
> Chris London
>

While it does sometimes happen that you do not need the value, it's not
particularly common and as such I don't think it's necessary to add
additional syntax for this case. It's fairly straightforward to abstract
this behavior away into a separate function used as follows:

    foreach (keys($iterable) as $key) { ... }

Where the keys function is defined as:

    function keys($iterable) {
        foreach ($iterable as $key => $_) {
            yield $key;
        }
    }

This is something you write once (with the slightly ugly $key => $_), but
can then always use to have clear code :) Of course, as already said in the
other mail, for arrays the keys() function already exists as `array_keys()`,
so you don't even have to write anything yourself.

Nikita
