PHP 不区分大小写的 in_array 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2166512/
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
PHP case-insensitive in_array function
提问by leepowers
Is it possible to do case-insensitive comparison when using the in_arrayfunction?
使用该in_array函数时是否可以进行不区分大小写的比较?
So with a source array like this:
因此,对于这样的源数组:
$a= array(
'one',
'two',
'three',
'four'
);
The following lookups would all return true:
以下查找都将返回 true:
in_array('one', $a);
in_array('two', $a);
in_array('ONE', $a);
in_array('fOUr', $a);
What function or set of functions would do the same? I don't think in_arrayitself can do this.
什么功能或一组功能会做同样的事情?我不认为in_array自己可以做到这一点。
采纳答案by ghostdog74
you can use preg_grep():
你可以使用preg_grep():
$a= array(
'one',
'two',
'three',
'four'
);
print_r( preg_grep( "/ONe/i" , $a ) );
回答by cletus
The obvious thing to do is just convert the search term to lowercase:
显而易见的事情就是将搜索词转换为小写:
if (in_array(strtolower($word), $array)) {
...
of course if there are uppercase letters in the array you'll need to do this first:
当然,如果数组中有大写字母,您需要先执行此操作:
$search_array = array_map('strtolower', $array);
and search that. There's no point in doing strtoloweron the whole array with every search.
并搜索那个。strtolower每次搜索都对整个数组进行操作是没有意义的。
Searching arrays however is linear. If you have a large array or you're going to do this a lot, it would be better to put the search terms in key of the array as this will be muchfaster access:
然而,搜索数组是线性的。如果你有一个大的数组或你打算这样做了很多,这将是更好地把搜索条件在阵列的关键,因为这将是很多快速访问:
$search_array = array_combine(array_map('strtolower', $a), $a);
then
然后
if ($search_array[strtolower($word)]) {
...
The only issue here is that array keys must be unique so if you have a collision (eg "One" and "one") you will lose all but one.
这里唯一的问题是数组键必须是唯一的,因此如果发生冲突(例如“一”和“一”),您将丢失除一个之外的所有键。
回答by Tyler Carter
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
From Documentation
从文档
回答by Gazler
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
Source: php.net in_array manual page.
回答by Mike Q
Say you want to use the in_array, here is how you can make the search case insensitive.
假设您想使用 in_array,这里是如何使搜索不区分大小写的方法。
Case insensitive in_array():
in_array() 不区分大小写:
foreach($searchKey as $key => $subkey) {
if (in_array(strtolower($subkey), array_map("strtolower", $subarray)))
{
echo "found";
}
}
Normal case sensitive:
正常区分大小写:
foreach($searchKey as $key => $subkey) {
if (in_array("$subkey", $subarray))
{
echo "found";
}
}
回答by Alex
The above is correct if we assume that arrays can contain only strings, but arrays can contain other arrays as well. Also in_array() function can accept an array for $needle, so strtolower($needle) is not going to work if $needle is an array and array_map('strtolower', $haystack) is not going to work if $haystack contains other arrays, but will result in "PHP warning: strtolower() expects parameter 1 to be string, array given".
如果我们假设数组只能包含字符串,那么上面是正确的,但数组也可以包含其他数组。此外 in_array() 函数可以接受 $needle 的数组,因此如果 $needle 是数组,则 strtolower($needle) 将不起作用,如果 $haystack 包含其他数组,则 array_map('strtolower', $haystack) 将不起作用数组,但会导致“PHP 警告:strtolower() 期望参数 1 为字符串,给定数组”。
Example:
例子:
$needle = array('p', 'H');
$haystack = array(array('p', 'H'), 'U');
So i created a helper class with the releveant methods, to make case-sensitive and case-insensitive in_array() checks. I am also using mb_strtolower() instead of strtolower(), so other encodings can be used. Here's the code:
所以我用相关方法创建了一个辅助类,以进行区分大小写和不区分大小写的 in_array() 检查。我还使用 mb_strtolower() 而不是 strtolower(),因此可以使用其他编码。这是代码:
class StringHelper {
public static function toLower($string, $encoding = 'UTF-8')
{
return mb_strtolower($string, $encoding);
}
/**
* Digs into all levels of an array and converts all string values to lowercase
*/
public static function arrayToLower($array)
{
foreach ($array as &$value) {
switch (true) {
case is_string($value):
$value = self::toLower($value);
break;
case is_array($value):
$value = self::arrayToLower($value);
break;
}
}
return $array;
}
/**
* Works like the built-in PHP in_array() function — Checks if a value exists in an array, but
* gives the option to choose how the comparison is done - case-sensitive or case-insensitive
*/
public static function inArray($needle, $haystack, $case = 'case-sensitive', $strict = false)
{
switch ($case) {
default:
case 'case-sensitive':
case 'cs':
return in_array($needle, $haystack, $strict);
break;
case 'case-insensitive':
case 'ci':
if (is_array($needle)) {
return in_array(self::arrayToLower($needle), self::arrayToLower($haystack), $strict);
} else {
return in_array(self::toLower($needle), self::arrayToLower($haystack), $strict);
}
break;
}
}
}
回答by Jake
I wrote a simple function to check for a insensitive value in an array the code is below.
我编写了一个简单的函数来检查数组中的不敏感值,代码如下。
function:
功能:
function in_array_insensitive($needle, $haystack) {
$needle = strtolower($needle);
foreach($haystack as $k => $v) {
$haystack[$k] = strtolower($v);
}
return in_array($needle, $haystack);
}
how to use:
如何使用:
$array = array('one', 'two', 'three', 'four');
var_dump(in_array_insensitive('fOUr', $array));
回答by Carlos Coelho
/**
* in_array function variant that performs case-insensitive comparison when needle is a string.
*
* @param mixed $needle
* @param array $haystack
* @param bool $strict
*
* @return bool
*/
function in_arrayi($needle, array $haystack, bool $strict = false): bool
{
if (is_string($needle)) {
$needle = strtolower($needle);
foreach ($haystack as $value) {
if (is_string($value)) {
if (strtolower($value) === $needle) {
return true;
}
}
}
return false;
}
return in_array($needle, $haystack, $strict);
}
/**
* in_array function variant that performs case-insensitive comparison when needle is a string.
* Multibyte version.
*
* @param mixed $needle
* @param array $haystack
* @param bool $strict
* @param string|null $encoding
*
* @return bool
*/
function mb_in_arrayi($needle, array $haystack, bool $strict = false, ?string $encoding = null): bool
{
if (null === $encoding) {
$encoding = mb_internal_encoding();
}
if (is_string($needle)) {
$needle = mb_strtolower($needle, $encoding);
foreach ($haystack as $value) {
if (is_string($value)) {
if (mb_strtolower($value, $encoding) === $needle) {
return true;
}
}
}
return false;
}
return in_array($needle, $haystack, $strict);
}
回答by omer
$a = [1 => 'funny', 3 => 'meshgaat', 15 => 'obi', 2 => 'OMER'];
$b = 'omer';
function checkArr($x,$array)
{
$arr = array_values($array);
$arrlength = count($arr);
$z = strtolower($x);
for ($i = 0; $i < $arrlength; $i++) {
if ($z == strtolower($arr[$i])) {
echo "yes";
}
}
};
checkArr($b, $a);
回答by user1077915
- in_array accepts these parameters : in_array(search,array,type)
- if the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
- so in order to make the search ignore the case, it would be enough to use it like this :
- in_array 接受这些参数: in_array(search,array,type)
- 如果搜索参数是字符串并且类型参数设置为 TRUE,则搜索区分大小写。
- 所以为了让搜索忽略大小写,像这样使用它就足够了:
$a = array( 'one', 'two', 'three', 'four' );
$a = array( '一', '二', '三', '四');
$b = in_array( 'ONE', $a, false );
$b = in_array( 'ONE', $a, false );

