Re: Two new functions array_first() and array_last()

From: Date: Wed, 18 Oct 2023 15:28:42 +0000
Subject: Re: Two new functions array_first() and array_last()
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-121398@lists.php.net to get a copy of this message
On Wed, 18 Oct 2023 at 17:05, Robert Landers <landers.robert@gmail.com> wrote: > > This is simply not true, 0 is not always the first key in a list, > especially after filtering it. Hence there is a need for this function > in the first place. Just to clarify this point: If 0 is not the first key, then it's not a list. After filtering a list, you get an array that may or may not be a list: https://3v4l.org/VegUr Regardless, to add my 2 cents, I'd keep things simple and vote for this signature: array_first(array $array): mixed array_last(array $array): mixed returning the first/last element, or null if the array is empty. Or, alternatively: array_first(array $array, mixed $default = null): mixed array_last(array $array, mixed $default = null): mixed returning $default if the array is empty. Both look fine to me. - Benjamin

« previous php.internals (#121398) next »