[RE] array_search
| From: | Gyozo Papp | Date: | Thu, 21 Jun 2001 08:04:24 +0000 |
| Subject: | [RE] array_search | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-54724@lists.php.net to get a copy of this message | ||
Hello,
you mixed array_search(...) with in_array(...).
<manual>
mixed array_search (mixed needle, array haystack, bool strict)
Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise.
</manual>
Therefore, in the 1st example it returns 0 and it was converted to FALSE by PHP, so you didn't
get the expected result. You should test if there is any match in the array:
if(($key = array_search("val1",$myarray)) !== false)
{
print "Value found at :". $key;
}
"Steen Rabol" <srj@jdpartner.dk> wrote in message news:9gpgpc$vqg$1@w153.z064000151.sjc-ca.dsl.cnc.net...
> Hi
>
> How to figure out of a value is found using array_search ?
>
> eg:
>
> $myarray = array("val1","val2","val3");
> if(array_search("val2",$myarray))
> {
> print "Value found";
> }
>
> the above will work, but...
>
> if(array_search("val1",$myarray))
> {
> print "Value found";
> }
>
> the above will not work as "val1" is key 0, how can I solve this ?
>
> Thanks in advance
>
> Steen
>
>
>
Papp Gyozo
- pgerzson@freestart.hu