Re: PHP 4.0 Bug #9337 Updated: make in_array return key, if searched value was found
| From: | Stig Venaas | Date: | Tue, 20 Feb 2001 07:53:41 +0000 |
| Subject: | Re: PHP 4.0 Bug #9337 Updated: make in_array return key, if searched value was found | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-47007@lists.php.net to get a copy of this message | ||
On Mon, Feb 19, 2001 at 12:02:48PM -0600, Andrei Zmievski wrote:
> On Mon, 19 Feb 2001, Jason Greene wrote:
> > > We can change in_array() to return the the key if it's found, and false
> > > if it's not, but imagine all the scripts it would break..
> > about 1/4th of mine : )
> >
> > Well I think the basic problem is that people are using in_array against its intention.
> > It is a Boolean function, and that really shouldn't be changed. We could create
> > another
> > function that is more designed for searching? perhaps array_find? I wouldn't mind
> > spending time on this if agreed upon.
>
> Could be done. The way I'd do it is have one php_in_array() C function
> that gets parameters passed through from in_array() and array_find() as
> well as a flag indicating whether to just return true/false or the
> actual key.
Not quite sure we need array_find(). If we can have in_array() set
the array pointer to point to the entry found, one can get the key
by using key(). If in_array() also searches from the pointer onwards,
one can find all entries with the value by doing:
reset($arr);
while (in_array($val, $arr)) {
$keys[] = key($arr);
next($arr);
}
I would prefer this rather then adding array_find() I think. If we
add array_find() I want similar functionality there. If not, I would
like an option for returning an array with all entries with the value.
Stig