Re: Proposal: addition of array_find() and array_find_key() functions
| From: | Casper Langemeijer | Date: | Fri, 02 Jun 2023 00:14:34 +0000 |
| Subject: | Re: Proposal: addition of array_find() and array_find_key() functions | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120507@lists.php.net to get a copy of this message | ||
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