如何在PHP中查找数组中的字符串?

时间:2022-08-14 19:25:09

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:

如果我想检查数组中是否存在此字符串($ str),我使用preg_match函数,如下所示:

$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?

如果我要比较两个或更多字符串,我该怎么做?

6 个解决方案

#1


34  

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

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

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

#2


6  

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

如果你只需要一个完全匹配,使用in_array($ str,$ array) - 它会更快。

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.

另一种方法是使用一个以字符串为关键字的关联数组,它应该以对数方式更快。怀疑你会看到它和线性搜索方法之间的巨大差异,但只有80个元素。

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

如果确实需要模式匹配,那么您需要遍历数组元素以使用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;
   }
}

#3


4  

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

preg_match期望字符串输入不是数组。如果您使用您描述的方法,您将收到:

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

警告:preg_match()要求参数2为字符串,在X行的LOCATION中给出数组

You want in_array:

你想要in_array:

if ( in_array ( $str , $array ) ) {
    echo 'It exists';
} else {
    echo 'Does not exist';
}

#4


3  

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

为什么不使用内置函数in_array? (http://www.php.net/in_array)

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

preg_match仅在查找另一个字符串中的子字符串时才有效。 (资源)

#5


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
}

#6


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.

函数in_array()仅检测数组元素的完整条目。如果要检测数组中的部分字符串,则必须检查每个元素。

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

#1


34  

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

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

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

#2


6  

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

如果你只需要一个完全匹配,使用in_array($ str,$ array) - 它会更快。

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.

另一种方法是使用一个以字符串为关键字的关联数组,它应该以对数方式更快。怀疑你会看到它和线性搜索方法之间的巨大差异,但只有80个元素。

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

如果确实需要模式匹配,那么您需要遍历数组元素以使用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;
   }
}

#3


4  

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

preg_match期望字符串输入不是数组。如果您使用您描述的方法,您将收到:

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

警告:preg_match()要求参数2为字符串,在X行的LOCATION中给出数组

You want in_array:

你想要in_array:

if ( in_array ( $str , $array ) ) {
    echo 'It exists';
} else {
    echo 'Does not exist';
}

#4


3  

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

为什么不使用内置函数in_array? (http://www.php.net/in_array)

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

preg_match仅在查找另一个字符串中的子字符串时才有效。 (资源)

#5


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
}

#6


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.

函数in_array()仅检测数组元素的完整条目。如果要检测数组中的部分字符串,则必须检查每个元素。

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