Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last()
| From: | Gabriel Caruso | Date: | Wed, 20 Jun 2018 12:20:24 +0000 |
| Subject: | Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last() | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102320@lists.php.net to get a copy of this message | ||
>
> don't think I'm getting all the mail I am supposed to. I hope this gets
> seen.
>
> I propose 2 functions *only* which I believe covers the use-cases that
> all 4 of these do and more, with shorter names, and the ability to
> discern whether the call succeeded or not.
>
> list($key, $value) = array_first($input);
> // $key will be null if the call failed
>
> list($key, $value) = array_last($input);
> // $key will be null if the call failed
>
> I have tested it with user-land functions and seems to work as
> intended in all success and failure conditions. Verification that it
> works as intended with internal functions is necessary but this seems
> a much better proposal to me.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
Hi Levi.
I have plans to bring an
array_find_first and array_find_last to the
next PHP 7.x. It will basically work:
$firstUserWithUsername = array_find_first(function (User $user): bool {
return $user->username !== null;
}, $users);
Just wanted to point that to alert that someone is working on that :)
--
Gabriel Caruso