Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a string in an array in PHP?

Tags:

arrays

php

I have an array:

$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) exists in the array or not, I use the preg_match function, which is like this:

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

if ($isExists) {
    echo "It exists";
} else {
    echo "It does not exist";
}

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.

And if I have two or more strings to compare, how can I do that?

like image 715
roa3 Avatar asked Feb 17 '09 08:02

roa3


People also ask

How do you check if a string is in an array in PHP?

PHP in_array() Function The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

What is Array_keys () used for in PHP?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

How do I find a value in an array?

If you need the index of the found element in the array, use findIndex() . If you need to find the index of a value, use indexOf() . (It's similar to findIndex() , but checks each element for equality with the value instead of using a testing function.)

What is Array_flip function in PHP?

This built-in function of PHP is used to exchange elements within an array, i.e., exchange all keys with their associated values in an array and vice-versa. We must remember that the values of the array need to be valid keys, i.e. they need to be either integer or string.


3 Answers

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

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

like image 140
Sam McAfee Avatar answered Oct 14 '22 01:10

Sam McAfee


If you just need an exact match, use in_array($str, $array) - it will be faster.

Another approach would be to use an associative array with your strings as the key, which should be logarithmically faster. Doubt you'll see a huge difference between that and the linear search approach with just 80 elements though.

If you do need a pattern match, then you'll need to loop over the array elements to use preg_match.


You edited the question to ask "what if you want to check for several strings?" - you'll need to loop over those strings, but you can stop as soon as you don't get a match...

$find=array("foo", "bar");
$found=count($find)>0; //ensure found is initialised as false when no terms
foreach($find as $term)
{
   if(!in_array($term, $array))
   {
        $found=false;
        break;
   }
}
like image 41
Paul Dixon Avatar answered Oct 14 '22 01:10

Paul Dixon


preg_match expects a string input not an array. If you use the method you described you will receive:

Warning: preg_match() expects parameter 2 to be string, array given in LOCATION on line X

You want in_array:

if ( in_array ( $str , $array ) ) {
    echo 'It exists';
} else {
    echo 'Does not exist';
}
like image 5
carbin Avatar answered Oct 14 '22 01:10

carbin