Re: Question about array_search

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Greg Donald wrote:

On Mon, 11 Oct 2004 09:07:13 -0700, Brian Dunning
<brian@xxxxxxxxxxxxxxxx> wrote:

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?


Do not use array_search() as a boolean test, it returns mixed results,
it will cause issues withthe zero index as younoted.  Instead use it
to match an array key, then see if the key exists with isset() and
compare it to the item you mean to add to the array.

You don't need all that, as it's already been pointed out.

$a = array_search('John',$names);

if($a === FALSE)
{ echo 'John is not in the array'; }
else
{ echo "John is at position $a in the array."; }

--

---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux