【问题标题】:PHP access value using case insensitive index使用不区分大小写的索引的 PHP 访问值
【发布时间】:2011-03-23 11:47:39
【问题描述】:

如何使用不区分大小写的索引访问 php 数组中的值。 喜欢

$x = array('a'=>'ddd');

使用 $x['A']; 访问它的任何函数;

【问题讨论】:

  • $x = array(strtoupper('a')=>'ddd'); $y = $x[strtoupper('A')];

标签: php arrays indexing


【解决方案1】:

这不是字符串/数组在 PHP 中的工作方式。

在 PHP 中,"a""A" 是两个不同的字符串
数组键可以是整数或字符串。

因此,$a["a"]$a["A"] 指向数组中的两个不同条目。


您有两种可能的解决方案:

  • 始终使用小写(或大写)键——这可能是最好的解决方案。
  • 或者在每次您想要访问一个条目时搜索所有数组以查找可能的匹配键 - 这是一个糟糕的解决方案,因为您必须循环遍历 (平均而言)数组的一半,而不是通过键进行快速访问。


在第一种情况下,每次要访问数组项时都必须使用strtolower()

$array[strtolower('KEY')] = 153;
echo $array[strtolower('KEY')];

在第二种情况下,这样的事情可能会起作用:
(嗯,这是一个未经测试的想法;但它可以作为您的基础)

if (isset($array['key'])) {
    // use the value -- found by key-access (fast)
}
else {
    // search for the key -- looping over the array (slow)
    foreach ($array as $upperKey => $value) {
        if (strtolower($upperKey) == 'key') {
            // You've found the correct key
            // => use the value
        }
    }
}

但是,这又是一个糟糕的解决方案!

【讨论】:

  • 循环中的“as”之前有一个额外的“$”。否则看起来不错!
【解决方案2】:

我会在这里使用 OOP,要么扩展 ArrayObject,要么实现 ArrayAcess,然后在偏移方法中使用相关的字符串函数来匹配任何一种情况。

这是 AFAIK 唯一 能够使用 $x['A']; 语法访问它的方式。

当然,我对优化速度没有兴趣。您应该决定是速度还是代码可读性是您的主要目标。

例子:

class MyArray extends ArrayObject {
    public function offsetGet($index) {
        if ($this->offsetExists($index)) {
            return parent::offsetGet($index);
        }
        if ($this->offsetExists(strtolower($index))) {
            return parent::offsetGet(strtolower($index));
        }
        if ($this->offsetExists(strtoupper($index))) {
            return parent::offsetGet(strtoupper($index));
        }
        return null;
    }

}

$x = array('a'=>'ddd');
$x = new MyArray($x);
echo $x['A'];

输出:

ddd

【讨论】:

  • 我想有人不喜欢我的回答。如果有人能告诉我它有什么问题,我将不胜感激。
  • 想法是什么?我的代码,它背后的原因,还是原来的问题?如果是我的代码,有哪些可能的改进方法?
【解决方案3】:
function array_keys_to_lower($array)
{
  $lc_array = array();

  foreach ($array as $key => $value)
  {
    $lc_array[strtolower($key)] = $value;
  }

  return $lc_array;
}

function get_value($array, $index)
{
  $lc_array = array_keys_to_lower($array);

  return $lc_array[strtolower($index)];
}

【讨论】:

    【解决方案4】:

    简单。
    不要这样做。

    混淆您自己的代码并不是一个好主意。
    只要遵守语法规则,你就没事了。

    【讨论】:

    • 这个线程中的人应该遵循这个建议。在任何事情上调用 array_change_key_case 都会导致任何代码库出现长期问题,因为需要在所有地方添加 strtolower 调用,并且很快它就会开始蔓延到代码库的不相关部分(可能不区分大小写)。
    【解决方案5】:

    你必须为此编写自己的函数,伪代码:

    function getElem(index, array){
      return array[toLower(index)];
    }
    

    【讨论】:

      【解决方案6】:
      $index = "a";
      
      $index = strtoupper($index);
      
      $ret = $a[$index];
      

      【讨论】:

        【解决方案7】:

        我猜在不改变你当前的数组使用情况的情况下会是这样的:

        $x = array('a'=>'ddd');
        
        echo isset($x['a']) ? $x['a'] : $x['A'];
        

        您可以使用如下功能:

        $x = array('a'=>'ddd');
        
        echo getIndex($x, 'a');
        
        // return either the value of $x['a'], $['A'] or null
        function getIndex($array, $index) {
            return isset($x[strtolower($index)]) ? $x[strtolower($index)] : (isset($x[strtoupper($index)]) ? $x[strtoupper($index)] : null);
        }
        

        【讨论】:

          【解决方案8】:

          这可能有点晚了,但我认为你想要完成的事情很容易完成:

          $key = 'Key';
          $array[$key] = 153;
          
          $array = array_change_key_case($array)
          echo $array[strtolower($key)];
          

          【讨论】:

            【解决方案9】:

            您可以使用array_change_key_case更改数组的键大小写,将所有键转换为小写或大写,让您高枕无忧。

            示例:

            $headers = getallheaders();
            $headers = array_change_key_case($headers, CASE_UPPER);
                if (isset($headers['AUTHENTICATION'])) {
                     // do your thing
                }
            

            http://php.net/manual/en/function.array-change-key-case.php

            【讨论】:

              猜你喜欢
              • 2017-06-30
              • 2011-01-16
              • 1970-01-01
              • 2011-03-31
              • 1970-01-01
              • 2014-03-25
              • 1970-01-01
              • 1970-01-01
              • 2020-06-11
              相关资源
              最近更新 更多