Re: [RFC] [Discussion] array_first() and array_last()

From: Date: Tue, 08 Apr 2025 18:24:24 +0000
Subject: Re: [RFC] [Discussion] array_first() and array_last()
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Mon, 7 Apr 2025 at 02:48, Ayesh Karunaratne <[email protected]> wrote:
>
> > On Mon, Apr 7, 2025 at 2:05 AM Bilge <[email protected]> wrote:
> > ... [snip] I suggest first proving there is a
> > legitimate need.
>
> I did a quick GitHub search for a common pattern of accessing an array
> value by using the array_key_first and array_key_last functions:
>
> ```php
> $value = $arr[array_key_first($results)];
> ```
>
>  - [array_key_first(: over 3,700 results[^1]
>  - [array_key_last(: over 4,300 results[^2]
>
> All of these hits can benefit from the proposed array_first and
> array_last.

(I used the wrong reply button earlier..)

I suspect this is just the tip of the iceberg. You should look for
reset() and end().
I get 336K when I look for "/(= |return |\(|\[)(reset|end)\(\$/ language:PHP".
I get 8.3K when I look for "/\[array_key_(first|last)\(/ language:PHP".

There are more matches for "/reset\(" and "/end\(", but we only want
matches where the return value is used.

And indeed I want to never have to use reset() and end() again!
I don't recall when I ever intentionally used the internal array
pointer. Only as a workaround to get a first or last element.

-- Andreas


Thread (22 messages)

« previous php.internals (#127074) next »