Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2001062

[PHP] [RE] array_search From: Gyozo Papp (pgerzson <email protected>)
Date: 06/21/01

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 <email protected>> wrote in message news:9gpgpc$vqg$1 <email protected>
> 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 <email protected>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe <email protected>
For additional commands, e-mail: php-general-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>