Hi,
I found myself wanting a feature of array_filter()
with which I can perform
filtering based on the array key instead of the value.
To scratch this itch, I decided to dive into C again and just add the
feature. My proposal is to add a third argument to array_filter()
that will
send the array keys to the callback.
function myfilter($value, $key)
{
return $key > 4;
}
array_filter([1, 2, 3, 4, 5, 6, 7], 'myfilter', true);
// returns: [6, 7]
The pull request can be found here: https://github.com/php/php-src/pull/287
If this is useful for the language as a whole, do let me know what else I
should do to champion it.
Thanks!
--
Tjerk
2013/2/21 Tjerk Anne Meesters datibbaw@php.net
Hi,
I found myself wanting a feature of
array_filter()
with which I can perform
filtering based on the array key instead of the value.To scratch this itch, I decided to dive into C again and just add the
feature. My proposal is to add a third argument toarray_filter()
that will
send the array keys to the callback.
Hi,
Why not simply always push the key to the callback?
Regards,
Sebastian
function myfilter($value, $key)
{
return $key > 4;
}array_filter([1, 2, 3, 4, 5, 6, 7], 'myfilter', true); // returns: [6, 7]
The pull request can be found here:
https://github.com/php/php-src/pull/287If this is useful for the language as a whole, do let me know what else I
should do to champion it.Thanks!
--
Tjerk
Hi Sebastian,
I would be fine with that too, but this might break existing code that uses
internal functions which behave differently when a second argument is
passed.
One might also make the argument that creating an additional ZVAL that's
never used in a callback is a waste of cpu.
I'm not sure about either of those counter arguments, but I added them for
anyone else to pick up :)
On Thu, Feb 21, 2013 at 4:02 PM, Sebastian Krebs krebs.seb@gmail.comwrote:
2013/2/21 Tjerk Anne Meesters datibbaw@php.net
Hi,
I found myself wanting a feature of
array_filter()
with which I can
perform
filtering based on the array key instead of the value.To scratch this itch, I decided to dive into C again and just add the
feature. My proposal is to add a third argument toarray_filter()
that
will
send the array keys to the callback.Hi,
Why not simply always push the key to the callback?
Regards,
Sebastianfunction myfilter($value, $key)
{
return $key > 4;
}array_filter([1, 2, 3, 4, 5, 6, 7], 'myfilter', true); // returns: [6, 7]
The pull request can be found here:
https://github.com/php/php-src/pull/287If this is useful for the language as a whole, do let me know what else I
should do to champion it.Thanks!
--
Tjerk
--
Tjerk
Hi,
I found myself wanting a feature of
array_filter()
with which I can perform
filtering based on the array key instead of the value.
With the operative word here being instead, keeping in line with the
numerous other array functions, would a new function called
array_filter_key() with the same signature as array_filter()
be more or
less appropriate?
The callback still being a single parameter function that receives the key
instead of the value.
Is there a big use case for having the key and value both passed to the
callback at the same time? (I can't think of anything off the top of my
head, but I'm sure someone would use it)
Hi,
I found myself wanting a feature of
array_filter()
with which I can
perform
filtering based on the array key instead of the value.With the operative word here being instead, keeping in line with the
numerous other array functions, would a new function called
array_filter_key() with the same signature asarray_filter()
be more or
less appropriate?
My use-case is as follows.
Imagine you have this array: ["ns.oauth" => "
http://specs.openid.net/extensions/oauth/1.0", "ns.alias3" => "
http://openid.net/srv/ax/1.0", "oauth.consumer" => "xxx", ...]
It's basically a bag of openid parameters. Now I'm tasked to find which
alias belongs to "http://specs.openid.net/extensions/oauth/1.0". For this I
need two pieces of information:
- I need to match the value for the namespace
- I need to make sure only keys that start with "ns." are chosen
Currently I use array_keys()
to find the keys whose values match the
namespace and then iterate over the results to find a key that matches the
second requirement.
The callback still being a single parameter function that receives the key
instead of the value.Is there a big use case for having the key and value both passed to the
callback at the same time? (I can't think of anything off the top of my
head, but I'm sure someone would use it)
--
Tjerk