Re: Proposal: addition of array_find() and array_find_key() functions
| From: | Hans Henrik Bergan | Date: | Fri, 02 Jun 2023 01:24:20 +0000 |
| Subject: | Re: Proposal: addition of array_find() and array_find_key() functions | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120508@lists.php.net to get a copy of this message | ||
sounds like array_find could be implemented by just adding a new flag
for array_filter's $mode:
ARRAY_FILTER_STOP_ON_FIRST_MATCH
or some such?
On Fri, 2 Jun 2023 at 02:15, Casper Langemeijer <langemeijer@php.net> wrote:
>
> On Thu, Jun 1, 2023, at 18:02, Janusz Szczypka wrote:
> > array_find(): This function would allow developers to find the first element in
> > an array that satisfies a specific condition. The condition would be defined by a callback
> > function.
>
> This would actually be an alternative to a simple foreach() with an if() and a break. As your
> example implementation makes clear.
>
> array_find() and array_find_key() would still do the same internally, but add a function call
> adding the overhead of pushing parameters to stack, switching the scope, passing return value back
> switch scope and pop from the callstack again. If parameters or return value are typed this adds
> even more overhead.
>
> I don't think there is any optimisation possible when this is done in C.
>
> To the proposer it probably feels that this is an array function currently missing, but instead
> it's probably better not to have array_find() because people might actually use it for larger
> datasets.
>
> Although I acknowledge that it will reduce the number of lines of PHP code it doesn't
> actually reduce the mental capacity to read and maintain code. I find the simple elemental building
> blocks of loops, conditionals and expressions easier and more expressive. In my experience this is
> especially so for less experienced developers.
>
> Then there is a problem of naming. array_search() and array_find(). To a new developer, how
> would they know which is which?
>
> Also there is the missing array_find_value_and_key() method. Because why should we be limited
> to either matching the key OR the value?
>
> Greetings, Casper
>