【问题标题】:How can I get in_array() case-insensitive?我怎样才能让 in_array() 不区分大小写?
【发布时间】:2015-05-31 08:51:22
【问题描述】:

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

如果数组中存在查找值,我希望它匹配而不区分大小写。

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"
}

【问题讨论】:

    标签: php arrays string


    【解决方案1】:

    如果您可以确保搜索词始终为小写,只需通过循环使用 array_map() 遍历所有值并将它们放入小写 strtolower() 将数组设置为小写,例如

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

    【讨论】:

    • 请解释反对意见,以便我改进答案
    • 我没有记下来.. 不知道是谁记的?但我会试试你的代码。
    • @user3436467 1. 你确定第一个参数是小写的 100% 吗? 2. 你的数组是多维的吗? 3.你的数组有多字节字符吗?
    • @user3436467:尝试将strtolower 也添加到第一个参数中,正如我在回答中所写的那样(如果您不确定此参数是小写的)。如果您有多字节,请改用mb_strtolower
    • 谢谢 Rizier123。我用数组print_r($array) 的样本更新了我的帖子
    【解决方案2】:

    您可以为此使用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!";
    

    【讨论】:

      【解决方案3】:

      如果@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';
      }
      

      【讨论】:

      • 感谢黑豹,我尝试了您的第二个解决方案,但没有成功。我用示例数组更新了我的帖子,以便您可以看到数组内容。我的查找值是 100% 小写
      • Rizier 的 array_map 解决方案没有理由不起作用。向我们展示你是如何实现它的。它必须工作。
      • if (in_array('www.paypal.com.au', array_map("strtolower", $array))) { echo 'exists'; } 在您的数组中有 WWW.PAYPAL.COM.AU 时有效。
      • 是的,似乎工作正常。我的错。还有一个地方我需要更新代码。
      【解决方案4】:
      function searchInArray($text, $array) {
          if (in_array(strtolower($text), array_map("strtolower", $array))) {
          return true;
          }
          return false;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 2012-10-28
        • 2011-01-11
        • 2011-11-06
        • 2021-12-16
        相关资源
        最近更新 更多