简体   繁体   English

如何让 in_array() 不区分大小写?

[英]How can I get in_array() case-insensitive?

Using this code to check if a value exists in an array but sometimes the array may contain values in uppercase.使用此代码检查数组中是否存在值,但有时数组可能包含大写的值。 My lookup value is always in lowercase .我的查找值总是小写 I cannot control the array data as its dynamically generated from a database so I need a solution that will ignore the case (uppercase/lowercase).我无法控制从数据库动态生成的数组数据,因此我需要一个可以忽略大小写(大写/小写)的解决方案。

If the lookup value exists in the array I would like it to match regardless of case sensitivity.如果查找值存在于数组中,我希望它匹配而不考虑大小写敏感。

if (in_array('lookupvalue', $array)) {
    // do something
}

output of var_dump($url); var_dump($url);输出var_dump($url); :

string(17) "www.paypal.com.au"

output of var_dump($sans); var_dump($sans);输出var_dump($sans); :

array(52) {
  [0]=>
  string(13) "WWW.PAYPAL.AT"
  [1]=>
  string(13) "WWW.PAYPAL.BE"
  [2]=>
  string(13) "WWW.PAYPAL.CA"
  [3]=>
  string(13) "WWW.PAYPAL.CH"
  [4]=>
  string(13) "WWW.PAYPAL.CL"
  [5]=>
  string(13) "WWW.PAYPAL.CN"
  [6]=>
  string(16) "WWW.PAYPAL.CO.ID"
  [7]=>
  string(16) "WWW.PAYPAL.CO.IL"
  [8]=>
  string(16) "WWW.PAYPAL.CO.IN"
  [9]=>
  string(19) "WWW.PAYPAL-MENA.COM"
  [10]=>
  string(16) "WWW.PAYPAL.CO.NZ"
  [11]=>
  string(16) "WWW.PAYPAL.CO.TH"
  [12]=>
  string(16) "WWW.PAYPAL.CO.UK"
  [13]=>
  string(16) "WWW.PAYPAL.CO.ZA"
  [14]=>
  string(17) "WWW.PAYPAL.COM.AR"
  [15]=>
  string(17) "WWW.PAYPAL.COM.AU"
  [16]=>
  string(17) "WWW.PAYPAL.COM.BR"
  [17]=>
  string(17) "WWW.PAYPAL.COM.HK"
  [18]=>
  string(17) "WWW.PAYPAL.COM.MX"
  [19]=>
  string(17) "WWW.PAYPAL.COM.MY"
  [20]=>
  string(17) "WWW.PAYPAL.COM.SA"
  [21]=>
  string(17) "WWW.PAYPAL.COM.SG"
  [22]=>
  string(17) "WWW.PAYPAL.COM.TR"
  [23]=>
  string(17) "WWW.PAYPAL.COM.TW"
  [24]=>
  string(17) "WWW.PAYPAL.COM.VE"
  [25]=>
  string(13) "WWW.PAYPAL.DE"
  [26]=>
  string(13) "WWW.PAYPAL.DK"
  [27]=>
  string(13) "WWW.PAYPAL.ES"
  [28]=>
  string(13) "WWW.PAYPAL.EU"
  [29]=>
  string(13) "WWW.PAYPAL.FI"
  [30]=>
  string(13) "WWW.PAYPAL.FR"
  [31]=>
  string(13) "WWW.PAYPAL.IE"
  [32]=>
  string(13) "WWW.PAYPAL.IT"
  [33]=>
  string(13) "WWW.PAYPAL.JP"
  [34]=>
  string(13) "WWW.PAYPAL.LU"
  [35]=>
  string(13) "WWW.PAYPAL.NL"
  [36]=>
  string(13) "WWW.PAYPAL.NO"
  [37]=>
  string(13) "WWW.PAYPAL.PH"
  [38]=>
  string(13) "WWW.PAYPAL.PL"
  [39]=>
  string(13) "WWW.PAYPAL.PT"
  [40]=>
  string(13) "WWW.PAYPAL.RU"
  [41]=>
  string(13) "WWW.PAYPAL.SE"
  [42]=>
  string(13) "WWW.PAYPAL.VN"
  [43]=>
  string(21) "WWW.THEPAYPALBLOG.COM"
  [44]=>
  string(25) "WWW.PAYPAL-DEUTSCHLAND.DE"
  [45]=>
  string(13) "WWW.PAYPAL.CO"
  [46]=>
  string(17) "WWW.PAYPAL.COM.PE"
  [47]=>
  string(17) "WWW.PAYPAL.COM.PT"
  [48]=>
  string(20) "WWW.PAYPAL-FRANCE.FR"
  [49]=>
  string(20) "WWW.PAYPAL-LATAM.COM"
  [50]=>
  string(23) "WWW.PAYPAL-MARKETING.PL"
  [51]=>
  string(15) "DEMO.PAYPAL.COM"
}

Well if you can make sure, that the search word is always in lowercase, just also put the array in lower case by looping through all values with array_map() and putting them in lowercase with strtolower() , eg好吧,如果你能确定,搜索词总是小写,也可以通过使用array_map()循环遍历所有值并使用strtolower()将它们放入小写来将数组置于小写,例如

if (in_array('lookupvalue', array_map("strtolower", $array))) {
// do something
}

You can use array_uintersect() for this:您可以array_uintersect()使用array_uintersect()

<?php

$haystack = [
  'apple',
  'banana',
  'cherry',
  'PineApple',
  'PEAR',
];

echo (
    array_uintersect(['pineapple'], $haystack, 'strcasecmp')
) ? "found\n" : "not found\n";

It computes the intersection of two arrays by means of a user defined function.它通过用户定义的函数计算两个数组的交集。 You feed it two array, one holding your needle, one being your haystack.你喂它两列,一个拿着你的针,一个是你的干草堆。 As a compare function you simply use php's strcasecmp() function.作为比较函数,您只需使用 php 的strcasecmp()函数。

So in the end your condition is a one-liner:所以最终你的情况是一个单线:

if ([] === array_uintersect([$needle], $haystack, 'strcasecmp'))

Which, since this is php, can be simplified to:其中,由于这是 php,可以简化为:

if (array_uintersect([$needle], $haystack, 'strcasecmp'))

Obviously you can also define a static function for this:显然你也可以为此定义一个静态函数:

function in_array_icase($needle, &$haystack) {
    return array_uintersect([$needle], $haystack, 'strcasecmp');
}

$jewelry = ['ring', 'bracelet', 'TaTToo', 'chain'];
if (in_array_icase('tattoo', $jewelry))
    echo "Yea!";

If @Rizier's array_map isn't good for you (don't know why), you can use preg_grep function.如果@Rizier 的array_map对你不好(不知道为什么),你可以使用preg_grep函数。

$array = array('Lookupvalue', 'second', 'third');
if (preg_grep('/^lookupVALUE$/i', $array)) {
    echo 'exists';
}

Notice to array_map :注意array_map
Maybe in Rizier's solution missing strtolower at the first parameter (if you can get it with capitals) to make it completely case-insensitive,也许在 Rizier 的解决方案中,第一个参数中缺少strtolower (如果您可以用大写字母获取它)以使其完全不区分大小写,

if (in_array(strtolower('LookupVALUE'), array_map('strtolower', $array))) {
    echo 'exists';
}
function searchInArray($text, $array) {
    if (in_array(strtolower($text), array_map("strtolower", $array))) {
    return true;
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM