[英]How can I get in_array() case-insensitive?
使用此代碼檢查數組中是否存在值,但有時數組可能包含大寫的值。 我的查找值總是小寫。 我無法控制從數據庫動態生成的數組數據,因此我需要一個可以忽略大小寫(大寫/小寫)的解決方案。
如果查找值存在於數組中,我希望它匹配而不考慮大小寫敏感。
if (in_array('lookupvalue', $array)) {
// do something
}
var_dump($url);
輸出var_dump($url);
:
string(17) "www.paypal.com.au"
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"
}
好吧,如果你能確定,搜索詞總是小寫,也可以通過使用array_map()
循環遍歷所有值並使用strtolower()
將它們放入小寫來將數組置於小寫,例如
if (in_array('lookupvalue', array_map("strtolower", $array))) {
// do something
}
您可以array_uintersect()
使用array_uintersect()
:
<?php
$haystack = [
'apple',
'banana',
'cherry',
'PineApple',
'PEAR',
];
echo (
array_uintersect(['pineapple'], $haystack, 'strcasecmp')
) ? "found\n" : "not found\n";
它通過用戶定義的函數計算兩個數組的交集。 你喂它兩列,一個拿着你的針,一個是你的干草堆。 作為比較函數,您只需使用 php 的strcasecmp()
函數。
所以最終你的情況是一個單線:
if ([] === array_uintersect([$needle], $haystack, 'strcasecmp'))
其中,由於這是 php,可以簡化為:
if (array_uintersect([$needle], $haystack, 'strcasecmp'))
顯然你也可以為此定義一個靜態函數:
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!";
如果@Rizier 的array_map
對你不好(不知道為什么),你可以使用preg_grep函數。
$array = array('Lookupvalue', 'second', 'third');
if (preg_grep('/^lookupVALUE$/i', $array)) {
echo 'exists';
}
注意array_map
:
也許在 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.