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

From: Date: Mon, 07 Apr 2025 19:31:23 +0000
Subject: Re: [RFC] [Discussion] array_first() and array_last()
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 06/04/2025 19:06, Juris Evertovskis wrote:
> On 2025-04-05 18:51, Niels Dossche wrote:
> Do you think it would be hard or wrong to add array_nth? I've had more
> trouble with that as the in-place implementation is usually pretty unreadable, e.g.
> 
>     array_slice(array_values($array), $offset, 1)[0] ?? null
> 
> I understand this need is less common, especially compared to array_first. But I
> also suspect that an in-engine implementation might have more performance gains compared to an
> userland one.
> 

This is not hard to add. For packed arrays this can be done in O(1) time, for non-packed arrays in
O(n) worst case.
If added, it's best to add the pair array_nth() & array_nth_key() together IMO.
However, it's not something I ever really needed nor something that I have seen a demand for.
I think this could be left for the future for follow-up work.

Kind regards
Niels


Thread (22 messages)

« previous php.internals (#127068) next »