roa3 roa3 - 2 years ago 84
PHP Question

How to find a string in an array in PHP?

Lets assume I have an array in PHP:

$array = array("apple", "banana", "cap", "dog", etc..) up to 80 values.

and a string variable:

$str = "abc";

If I want to check whether this string (
) is exists in the array or not, I use preg_match function. Which is like this:

$isExists = preg_match("/$str/", $array);

if ($isExists){

echo "Its exists";
} else {

echo "Not exixts"

} ;

Is it the correct way? If the array grows bigger, will it be very slow? Is there any other method? I am trying to scaling down my database traffic. Hope you guys can help..

If i have two or more string to compare? How to do that?

Answer Source
 bool in_array  ( mixed $needle  , array $haystack  [, bool $strict  ] )

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download