Re: [discussion] add array_usearch()
| From: | Tjerk Meesters | Date: | Fri, 07 Feb 2014 00:26:11 +0000 |
| Subject: | Re: [discussion] add array_usearch() | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-72355@lists.php.net to get a copy of this message | ||
Hi Chris,
On Fri, Feb 7, 2014 at 4:28 AM, Chris Wright <daverandom@php.net> wrote:
> On 6 February 2014 19:55, Tjerk Meesters <tjerk.meesters@gmail.com> 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.
>
I was talking about memory growth, not runtime :)
>
> 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.
>
I'm not wildly enthusiastic about another magic flags parameter, but it's
an acceptable compromise for me. The slight advantage there is that
in_array() would be bestowed upon the ability to take a callback function
as well. Something like:
if (in_array('is_numeric', $array, SEARCH_NEEDLE_IS_CALLBACK)) { ... }
>
> Thanks, Chris
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
--
Tjerk