Re: array_search
| From: | Lars Torben Wilson | Date: | Tue, 26 Feb 2002 09:53:29 +0000 |
| Subject: | Re: array_search | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-86208@lists.php.net to get a copy of this message | ||
On Tue, 2002-02-26 at 01:29, Roger Keays wrote:
> Hi,
>
> Can anybody explain why the output of this script is
>
> not found
> Found!
Yup. array_search() returns the key of the found object, as noted in the
docs (http://www.php.net/array_search). Since the first test has the
searched-for field at index 0, array_search() will return an int. You're
doing a loose (==) test against TRUE (a boolean, not an int), so you
get 'not found.'. In the second example, the searched-for item is at
index 1, which does loosely evaluate to TRUE.
I think the function you're looking for is in_array():
http://www.php.net/in_array
Cheers,
Torben
> Here is the script...
>
> $legalfields = array("reasonForRepair");
> if (array_search("reasonForRepair", $legalfields) == TRUE) {
> echo "Found!<br>";
> } else {
> echo "not found<br>";
> }
> $legalfields = array("foo", "reasonForRepair");
> if (array_search("reasonForRepair", $legalfields) == TRUE) {
> echo "Found!";
> } else {
> echo "not found";
> }
> ?>
>
> Thanks in advance,
>
> Roger
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Torben Wilson <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506