Re: array_reject() as counterpart of array_filter()
| From: | Josh Bruce | Date: | Mon, 31 Aug 2020 13:52:46 +0000 |
| Subject: | Re: array_reject() as counterpart of array_filter() | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111737@lists.php.net to get a copy of this message | ||
Just to confirm
array_filter(“!is_int”, $collection)
Would result in a collection of only non-integers??
I do think there’s something to be said for the communication of intent without syntax.
array_without or array_reject reads easier to me than making sure to watch for the bang.
Cheers,
Josh
>> On Aug 30, 2020, at 6:55 PM, Larry Garfield <larry@garfieldtech.com> wrote:
>>
>> On Sun, Aug 30, 2020, at 9:38 AM, David Rodrigues wrote:
>> Currently we have array_filter(), but sometimes we need an inverse function
>> like array_reject().
>> array_reject('is_null', [ 1, 2, null, 3 ]); // [ 1, 2, 3 ]
>> It could be easily implemented with:
>> function array_reverse($fn, $arr) { return array_filter(fn($item) =>
>> !$fn($item), $arr); }
>> Anyway, I think that It should be implemented by core, once that we have
>> array_filter().
>
> Any boolean function can be inverted with a simple ! in a short closure. I don't really
> see a need to do that in C.
>
> --Larry Garfield
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php