How to find a string in an array in PHP?

前端 未结 6 433
孤独总比滥情好
孤独总比滥情好 2020-12-10 05:11

I have an array:

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

and a string variable:

$st         


        
相关标签:
6条回答
  • 2020-12-10 05:23

    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;
       }
    }
    
    0 讨论(0)
  • 2020-12-10 05:27

    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
    }
    
    0 讨论(0)
  • 2020-12-10 05:33

    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';
    }
    
    0 讨论(0)
  • 2020-12-10 05:33

    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)

    0 讨论(0)
  • 2020-12-10 05:44
     bool in_array  ( mixed $needle  , array $haystack  [, bool $strict  ] )
    

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

    0 讨论(0)
  • 2020-12-10 05:47

    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"; 
      }
    }
    
    0 讨论(0)
提交回复
热议问题