Re: [discussion] add array_usearch()
On 6 February 2014 19:55, Tjerk Meesters <[email protected]> wrote:
> Consider the following examples for finding a value in an array:
>
> array_search($value, $array, true);
>
> current(array_keys($array, $value, true));
>
> key(array_filter($array, function($item) use ($value) {
> return $item === $value;
> }));
>
> Perhaps I've overlooked one way, but that's already three ways of doing
> something similar; the difference lies in the memory behaviour, the first
> being O(1) whereas the other two are O(n).
I'm not sure where you got this statement from. array_search() is
still O(n) in the worst case (when the needle is not found), which is
the best you can hope for with such an operation. It's true that the
other two are *always* O(n) whereas array_search() *could* return
sooner than a complete iteration if the needle is found before the
last element.
My initial reaction to this is one of "ye gods, not another array_*
function", but I wouldn't be against altering the existing
array_search() so that the third parameter is a bitmask of flags, one
of which could indicate that the search should be case insensitive.
This could be done in a BC-compliant manner if the flag that indicates
strict comparisons has a value of 1.
Thanks, Chris
Thread (11 messages)