php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77532 foreach iteration over keys without values
Submitted: 2019-01-28 00:24 UTC Modified: 2019-01-28 16:24 UTC
From: jerry at jmweb dot net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2019-01-28 00:24 UTC] jerry at jmweb dot net
Description:
------------
Too often I do something like:

foreach( $array as $key => $noop )
{
  // do something with $key only
}

Why not use array_keys? Well, I guess to avoid the overhead of calling a function. It would be super if we could simply do:

foreach( $array as $key => null )
{
  // do something with $key only
}

I am not 100% set on using null, but I will let the community decide on the specifics.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-28 00:38 UTC] danack@php.net
> but I will let the community decide on the specifics.

How about using '$noop' ?

In which case, the feature is complete.
 [2019-01-28 00:53 UTC] jerry at jmweb dot net
LOL. Obviously using the variable $noop works but I was hoping that the feature would 1) allow looping through keys w/out needing a variable for unused values and 2) possibly a speed bump over using array_keys.
 [2019-01-28 01:08 UTC] stas@php.net
-Package: PHP Language Specification +Package: Scripting Engine problem
 [2019-01-28 11:40 UTC] girgias@php.net
-Summary: foreach just over keys +Summary: foreach iteration over keys without values -Operating System: n/a +Operating System:
 [2019-01-28 11:40 UTC] girgias@php.net
There was an RFC [1] started 3 years ago which hasn't seen any activity so maybe we should look back into it?

[1]: https://wiki.php.net/rfc/foreach_void
 [2019-01-28 16:24 UTC] jerry at jmweb dot net
What A fantastic RFC especially with no BC breaks! How can we get the RFC active again? I do not have experience with RFCs nor with C code so I can't verify if the patches are still compatible with the current codebase.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 00:01:23 2019 UTC