Re: in_array() 4.0.0 -> 4.0.2
| From: | David Newcomb | Date: | Tue, 12 Sep 2000 10:07:25 +0000 |
| Subject: | Re: in_array() 4.0.0 -> 4.0.2 | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-16316@lists.php.net to get a copy of this message | ||
> Andrei Zmievski wrote:
> >
> > > The purpose of in_array() is to say if an item is in the array,
> > > with a rather nice side effect of re-positioning the array pointer
> > > to that element.
> > >
> > > How are we to find the position of the element?
> > >
> > > I don't want to have to do an expensive for loop every time I
> > > want to get an index.
> > >
> > > Short of coding up a my_in_array() what are the alternatives?
> >
> > Hmm, I didn't realize people relied on that behavior. It's possible to
> > change it back to the old one, but still implement a check that if the
> > element is not found the pointer is reset to the beginning of the array,
> > but I guess I'd like to hear more opinions about this before doing any
> > changes.
>
> OK, then, as long as you are fising for opinions. I didn't know the
> feature existed and so implemented my own "my_in_array". Wish I had
> known it was built-in: faster and easier. Makes a lot of sense b/c a
> good percentage of the time that you search for a value you want to know
> what the key is;
I have done some profiling on some of my code and found that in_array
is an expensive operation. Heaven only knows how heavy my_in_array()
must be!
> on the other hand I have not yet been in a situation
> where I was worried about moving the pointer by in_array.
I must say that I'm with Andrei Zmievski on this one. I have not come
across a situration which I have used next(),current(),prev() or any
other function that moves the internal array pointer (except in_array() ;)
> Of course, using the configurable approach, there could be an optional
> third argument for "don't move the pointer".
This will be a forth option! Although not published in the
documentation, there has been added a boolean "strict" parameter
to make the in_array() comparison more strict. Here is the reference:
In php-4.0.2/ext/standard/array.c line 1036
/* {{{ proto bool in_array(mixed needle, array haystack [, bool strict])
Checks if the given value exists in the array */
PHP_FUNCTION(in_array)
{
Cheers,
David.