Re: Question about array_search

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

 



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.

There lots of other ways too, possibly more overhead, but less code. 
For example you could just add the item to the array blindly, then run
array_unique() on the array.


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.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