roa3 roa3 - 1 year ago 35
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?

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