How can I get in_array () case insensitive?

Using this code to check if a value exists in an array, but sometimes the array can contain uppercase values. My search value is always lowercase . I can not control the data of the array, because it is dynamically generated from the database, so I need a solution that will ignore the case (in upper and lower case).

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); output var_dump($url); :

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

var_dump($sans); output 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" } 
+1
string arrays php
May 31 '15 at 8:51
source share
3 answers

Well, if you can make sure the search word is always lowercase, just put the array in lowercase, going through all the values ​​with array_map() and putting them in lowercase with strtolower() , e.g.

 if (in_array('lookupvalue', array_map("strtolower", $array))) { // do something } 
+4
May 31 '15 at 8:53
source share

You can use array_uintersect() to do this:

 <?php $haystack = [ 'apple', 'banana', 'cherry', 'PineApple', 'PEAR', ]; echo ( array_uintersect(['pineapple'], $haystack, 'strcasecmp') ) ? "found\n" : "not found\n"; 

It calculates the intersection of two arrays using a user-defined function. You feed him two arrays, one holding his needle, and one holding a haystack. As a comparison function, you simply use the php strcasecmp() function.

So, ultimately your state is single-line:

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

Which, since this is php, can be simplified to:

 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!"; 
+3
May 31 '15 at 9:07
source share

If @Rizier array_map not suitable for you (I don’t know why), you can use preg_grep .

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

Pay attention to array_map :
Perhaps in the Rizier solution there is no strtolower on the first parameter (if you can get it with capitals) to make it completely case insensitive,

 if (in_array(strtolower('LookupVALUE'), array_map('strtolower', $array))) { echo 'exists'; } 
+2
May 31 '15 at 8:56
source share



All Articles