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

From: Date: Wed, 18 Oct 2023 15:37:17 +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-121399@lists.php.net to get a copy of this message
On Wed, Oct 18, 2023 at 12:04 PM Robert Landers <landers.robert@gmail.com> wrote: > > I completely understand what you're saying and I don't disagree with the > thought process. What I disagree with is your statement that you will > always use array_first() together with array_key_first(). When talking > about standard lists (indexed from 0 to upper-bound), array_first() is an > order of magnitude more useful than array_key_first() as we always know > what the first key is: 0. > > 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. > To give back on the same format, "this is simply not true". Here are the facts: - https://www.php.net/manual/en/function.array-is-list.php - Determines if the given array is a list. An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1." - https://3v4l.org/JZS9N#v8.2.11 At this point I have nothing else to add to this conversation. You will either accept that it is actually possible to use array_first() without using array_key_first() or you'll keep believing that your knowledge and experience is the only correct one. I believe my reservations have been registered, I would not like to have array_first() behave in a non-obvious way (i.e. involving array $key) because it's useful for Fibers users. I believe that writing your own wrapper that always returns key and value simultaneously is easy and straight-forward and may even make it into the core in the future. Rather than trying to solve deep convoluted issues on version 1, I believe PHP could introduce the simplest version of the function first and later on add an array_first_key_value() in the future if needed. In fact, I encourage you to propose an RFC for array_first_key_value() as a solution for the problem you're raising. -- Marco Deleu

« previous php.internals (#121399) next »