如何获得不区分大小写的in_array()?

use*_*467 2 php arrays string

使用此代码检查数组中是否存在值,但有时该数组可能包含大写的值。我的查找值始终为小写。我无法控制数组数据,因为它是从数据库动态生成的,因此我需要一个可以忽略大小写(大写/小写)的解决方案。

如果数组中存在查找值,则无论大小写如何,我都希望其匹配。

if (in_array('lookupvalue', $array)) {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

输出var_dump($url);

string(17) "www.paypal.com.au"
Run Code Online (Sandbox Code Playgroud)

输出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"
}
Run Code Online (Sandbox Code Playgroud)

Riz*_*123 7

好吧,如果您可以确定搜索词始终是小写字母,只需通过遍历所有值array_map()并将它们放在小写字母中,也可以将数组设置为小写字母strtolower(),例如

if (in_array('lookupvalue', array_map("strtolower", $array))) {
// do something
}
Run Code Online (Sandbox Code Playgroud)


ark*_*cha 6

您可以array_uintersect()为此使用:

<?php

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

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

它通过用户定义的函数计算两个数组的交集。你喂它两列,一个拿着你的针,一个是你的干草堆。作为比较函数,您只需使用 php 的strcasecmp()函数。

所以最终你的情况是单线的:

if ([] === array_uintersect([$needle], $haystack, 'strcasecmp'))
Run Code Online (Sandbox Code Playgroud)

其中,由于这是 php,可以简化为:

if (array_uintersect([$needle], $haystack, 'strcasecmp'))
Run Code Online (Sandbox Code Playgroud)

显然你也可以为此定义一个静态函数:

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!";
Run Code Online (Sandbox Code Playgroud)