如何获得不区分大小写的in_array()? [英] How can I get in_array() case-insensitive?

查看:58
本文介绍了如何获得不区分大小写的in_array()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

var_dump($ url); :

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

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()将其小写,例如

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(), e.g.

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

这篇关于如何获得不区分大小写的in_array()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆