12

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?

1

6 Answers 6

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

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

Sign up to request clarification or add additional context in comments.

Comments

6

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;
   }
}

Comments

5

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';
}

1 Comment

the string like this $var "id1;id2";
3

Why not use the built-in function in_array? (http://www.php.net/in_array)

preg_match will only work when looking for a substring in another string. (source)

Comments

2

If you have more than one value you could either test every value separatly:

if (in_array($str1, $array) && in_array($str2, $array) && in_array($str3, $array) /* … */) {
    // every string is element of the array
    // replace AND operator (`&&`) by OR operator (`||`) to check
    // if at least one of the strings is element of the array
}

Or you could do an intersection of both the strings and the array:

$strings = array($str1, $str2, $str3, /* … */);
if (count(array_intersect($strings, $array)) == count($strings)) {
    // every string is element of the array
    // remove "== count($strings)" to check if at least one of the strings is element
    // of the array
}

Comments

0

The function in_array() only detects complete entries if an array element. If you wish to detect a partial string within an array, each element must be inspected.

foreach ($array AS $this_string) {
  if (preg_match("/(!)/", $this_string)) {
    echo "It exists"; 
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.