Re: Question about array_search
| From: | Matt M. | Date: | Mon, 11 Oct 2004 16:32:29 +0000 |
| Subject: | Re: Question about array_search | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-199200@lists.php.net to get a copy of this message | ||
> Say I'm trying to add a value to an array, only if it's not already in
> there somewhere; so I do an array_search to see. The problem is that if
> the item is at index 0 in the array, array_search gives the same answer
> as if it's not in there at all. How does one circumvent this potential
> pitfall?
I think this would work (I could be wrong)
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 =>
'red');
if(($key = array_search('bluesf', $array)) !== FALSE) {
echo $key;
}
if(($key = array_search('blue', $array)) !== FALSE) {
echo $key;
}