# Pull request for array_filter() improvement

- Thread: https://externals.io/message/66089
- Original archive: https://news-web.php.net/php.internals/66089
- Started by Tjerk Anne Meesters on 2013-02-21T07:08:58+00:00
- Messages: 5
- 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

---

## #66089 — Tjerk Anne Meesters — 2013-02-21T07:08:58+00:00

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

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

---

## #66091 — Sebastian Krebs — 2013-02-21T08:02:44+00:00

URL: https://externals.io/message/66089#66091 · In reply to: #66089

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 to `array_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/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
>



-- 
github.com/KingCrunch

---

## #66092 — Tjerk Anne Meesters — 2013-02-21T08:08:06+00:00

URL: https://externals.io/message/66089#66092 · In reply to: #66091

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.com>wrote:

>
>
>
> 2013/2/21 Tjerk Anne Meesters <datibbaw@php.net>
>
> > […]
>
> Hi,
>
> Why not simply _always_ push the key to the callback?
>
> Regards,
> Sebastian
>
>
> > […]
>
>
>
> --
> github.com/KingCrunch
>



-- 
--
Tjerk

---

## #66094 — Leigh — 2013-02-21T09:31:07+00:00

URL: https://externals.io/message/66089#66094 · In reply to: #66089

> 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)

---

## #66098 — Tjerk Anne Meesters — 2013-02-21T09:57:11+00:00

URL: https://externals.io/message/66089#66098 · In reply to: #66094

>
> 
>
> > […]
> 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?
>

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:

1. I need to match the value for the namespace
2. 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
