roa3 roa3 - 6 months ago 13
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 (
$str
) 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..

AFTER GET THE ANSWER:
If i have two or more string to compare? How to do that?

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

http://php.net/manual/en/function.in-array.php