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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jerry at jmweb dot net
New email:
PHP Version: OS:

 

 [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: Sat Dec 07 06:01:24 2019 UTC