Closed
Description
Scenario
I want be able to filter an array by key or both (key, value)
/// DOESN"T WORK :(
(new ArrayCollection(['italian' => 'pizza', 'turkish' => 'kebab']))
->filter(function($k, $v) {
return 'turkish' === $k;
})
// WORKSS!1!!
array_filter(['italian' => 'pizza', 'turkish' => 'kebab'], function($k, $v) {
return 'turkish' === $k;
}, ARRAY_FILTER_USE_BOTH);
Suggested Solution
Since PHP 5.6 we are able to specify a flag on array_filter
which allows to filter the element of the array by passing to the closure that filter the array, key, value or both
array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )
Flag determining what arguments are sent to callback:
ARRAY_FILTER_USE_KEY - pass key as the only argument to callback instead of the value
ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value
Default is 0 which will pass value as the only argument to callback instead.
Suggested Implementation
At the moment ArrayCollection#filter uses the default value 0 which makes the flag feature impossible to use.
- public function filter(Closure $p)
+ public function filter(Closure $p, int $flag = 0)
{
- return $this->createFrom(array_filter($this->elements, $p));
+ return $this->createFrom(array_filter($this->elements, $p, $flag));
}
Concerns
I understand that exposing lower level APIs to an higher level of abstraction is not probably a good idea. Yet, the additional feature would be useful overall to the client who uses the method
If you provide some feedback I can submit a PR