【问题标题】:PHP array_search consistently returns first key of array [duplicate]PHP array_search 始终返回数组的第一个键[重复]
【发布时间】:2012-10-26 21:53:59
【问题描述】:

我最近在我的代码中使用 array_search 函数时发现了一些问题。我正在数组“$allcraftatts”中搜索值“sharp”。我试图通过设置两行实验来隔离问题:

$testcopy=$allcraftatts;
$testsharp=array_search("sharp", $testcopy);

使用“print_r(get_defined_vars());”后来,我得到了这个结果:

[testcopy] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                    [6] => Sharp Stone
                    [7] => Sharp Stones
                    [8] => stone
                    [9] => object
                    [10] => sharp
                    [11] => hard
                    [12] => 0
                    [13] => 0
                    [14] => 0
                    [15] => 0
                    [16] => 0
                    [17] => 0
                    [18] => 0
                )

[testsharp] => 0

我确保不会在任何其他时间修改这些变量。

现在,如果我将代码更改为

$testcopy=$allcraftatts;
unset($testcopy[0]);
$testsharp=array_search("sharp", $testcopy);

它返回“1”。

这让我相信它总是返回数组中的第一个键。

这让我很困惑!这是让您担心语言本身有问题的错误之一。不管这是多么令人怀疑,我实际上最终还是被驱使去查看 PHP 源代码是否有问题,但不幸的是无法理解它。

看到这么简单的函数,我肯定会被不可避免的简单答案彻底羞辱,但此时,我只想得到一个答案。

【问题讨论】:

  • 您使用的是什么版本的 PHP?您还可以向我们展示var_dump 在这些变量上的输出而不是print_r 吗?它会显示你是否有多余的空格(这是人们使用array_search 时经常遇到的问题)

标签: php arrays function


【解决方案1】:

array_search 在搜索过程中使用== 比较值

形成 PHP 文档

如果第三个参数 strict 设置为 TRUE,则 array_search() 函数将在 haystack 中搜索相同的元素。这意味着它还将检查大海捞针的类型,并且对象必须是相同的实例。

因为第一个元素是0,所以在搜索过程中字符串被转换为0

简单测试

var_dump("sharp" == 0); //true
var_dump("sharp" === 0); //false

解决方案使用strict 选项来搜索相同的值

$testsharp = array_search("sharp", $testcopy,true);
                                               ^---- Strict Option

var_dump($testsharp);

输出

10

【讨论】:

    【解决方案2】:

    如果搜索到的键之前的任何键是数字零,则返回该键,因为它正在执行由数组的数据类型支配的“松散”匹配,并且“sharp”(如果转换为int)计为零。使用严格检查,找到正确的值。

    否则,通过执行

    $testcopy = array_map('strval', $testcopy);
    

    为了将值转换为字符串,它也适用于“松散”检查。

    【讨论】:

      【解决方案3】:

      欢迎来到松散打字的美妙世界。在 php 中,array_search 默认为非严格比较(“==”),但您可以添加第三个参数来强制严格比较(“===”)。您几乎总是想要严格,尽管有时非严格是正确的操作。

      查看以下内容:

      $allcraftatts = array(0, 0, 0, 0, 0, 0, "Sharp Stone", "Sharp Stones", "stone", new stdClass(), "sharp", "hard", 0, 0, 0, 0, 0,0 ,0);
      $testcopy=$allcraftatts;
      $testsharp=array_search("sharp", $testcopy);
      $testsharpStrict=array_search("sharp", $testcopy, true);
      
      print_r(get_defined_vars());                                                                                                                                                                                                                        
      
      if(0 == "sharp"){
          echo "true for == \n";
      }else{
          echo "false == \n";
      }
      if(0 === "sharp"){
          echo "true for === \n";
      }else{
          echo "false === \n";
      }
      

      和输出:

      [testcopy] => Array
              (
                  [0] => 0
                  [1] => 0
                  [2] => 0
                  [3] => 0
                  [4] => 0
                  [5] => 0
                  [6] => Sharp Stone
                  [7] => Sharp Stones
                  [8] => stone
                  [9] => stdClass Object
                      (
                      )
      
                  [10] => sharp
                  [11] => hard
                  [12] => 0
                  [13] => 0
                  [14] => 0
                  [15] => 0
                  [16] => 0
                  [17] => 0
                  [18] => 0
              )
      
          [testsharp] => 0
          [testsharpStrict] => 10
      )
      true for == 
      false === 
      

      【讨论】:

        猜你喜欢
        • 2016-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-05
        • 1970-01-01
        • 1970-01-01
        • 2018-02-10
        相关资源
        最近更新 更多