如何在 PHP 中的数组中查找字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/555975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a string in an array in PHP?
提问by roa3
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_matchfunction, 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?
如果我有两个或更多字符串要比较,我该怎么做?
回答by Sam McAfee
bool in_array ( mixed $needle , array $haystack [, bool $strict ] )
回答by Paul Dixon
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 doneed 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;
}
}
回答by zi3guw
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';
}
回答by JohnnyPark
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 仅在查找另一个字符串中的子字符串时有效。(来源)
回答by Gumbo
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 intersectionof 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
}
回答by MissionNext
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";
}
}

