Re: Two new functions array_first() and array_last()
| From: | Deleu | Date: | Wed, 18 Oct 2023 03:25:46 +0000 |
| Subject: | Re: Two new functions array_first() and array_last() | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121368@lists.php.net to get a copy of this message | ||
On Tue, Oct 17, 2023 at 3:43 PM Brandon Jackson <brandonja991@gmail..com>
wrote:
> > There is also a technique to make the return value
[$key => $value]
> >
> instead of just a value, but this loses simplicity.
>
> Hmm, since the naming array_first and array_last doesn't clarify that
> it's returning a key or a value. What if it returned both as ?[key,
> value].
>
> That opens quite a few use possibilities:
> $first = array_first($array);
> $value = $first[1] ?? throw new Exception();
>
> [,$value] = array_first($array) ?? [null, null];
> [,$value] = array_first($array) ?? throw new Exception();
>
This function signature can be accomplished by userland once we have
array_key_first() and array_first(). It's much better to keep
array_first() as simple as possible and let everyone build their own
approach to go about it since we have so many approaches.
--
Marco Deleu