【问题标题】:[PHP]: What does array_search() return if nothing was found?[PHP]:如果没有找到,array_search() 会返回什么?
【发布时间】:2011-02-04 14:44:25
【问题描述】:

如果没有找到,array_search() 会返回什么?

我需要以下逻辑:

$found = array_search($needle, $haystack);

if($found){
  //do stuff
} else {
  //do different stuff
}

【问题讨论】:

  • 试一试看结果比问快。
  • 检查结果 is_int() 看到它返回一个键..
  • @LucM google 这个问题比尝试要快。

标签: php arrays search


【解决方案1】:

引用array_search()手册页

如果是则返回针的键 在数组中找到,FALSE 否则


这意味着你必须使用类似的东西:

$found = array_search($needle, $haystack);

if ($found !== false) {
    // do stuff
    // when found
} else {
    // do different stuff
    // when not found
}

注意我使用了!== 运算符,它进行类型敏感的比较;有关详细信息,请参阅Comparison OperatorsType JugglingConverting to boolean ;-)

【讨论】:

  • Note I used the !== operator, that does a type-sensitive comparison - 这正是问题所在。 0 被评估为假...谢谢
  • 不客气 :-) ;;我已经编辑了答案以添加指向手册其他相关页面的链接,顺便说一句:-)
  • 它现在返回NULL。您可能想更新您的答案。
  • @AmalMurali - array_search() 仅在提供无效参数时返回 NULL,例如一个不是数组的变量。如果您提供有效参数,如果在数组中找不到该值,它将返回false
  • @DerekAdair 我知道这个问题已经很老了,但this answer 中的更多信息供将来参考。
【解决方案2】:

如果您只是检查值是否存在,in_array 是可行的方法。

【讨论】:

    【解决方案3】:

    必须非常小心区分item found with index 0not found 的情况。

    在第一种情况下array_search 返回整数0,在第二种情况下返回布尔值false

    危险区域,因为如果我们不注意,很容易在if 测试中将0 和false 视为同一事物。 ⚠

    如果你想区分两者,你必须使用if ($i !== false), 而不是if ($i)if ($i != false)。为什么?请参阅以下示例:

    例子:

    $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
    
    $i = array_search('red', $array);   
    var_dump($i);                      // int(1)
    echo ($i !== false) ? $i : -1;     // 1
    
    $i = array_search('blue', $array); 
    var_dump($i);                      // int(0)
    echo ($i !== false) ? $i : -1;     // 0      // CORRECT
    echo ($i != false) ? $i : -1;      // -1     // WRONG! don't use:  if ($i) ...
    echo ($i) ? $i : -1;               // -1     // WRONG! don't use:  if ($i != false) ...
    
    $i = array_search('blueee', $array);
    var_dump($i);                      // bool(false)
    echo ($i !== false) ? $i : -1;     // -1, i.e. not found
    
    

    【讨论】:

      【解决方案4】:

      根据http://php.net/manual/en/function.array-search.php的官方文档:

      警告此函数可能返回布尔值 FALSE,但也可能返回 计算结果为 FALSE 的非布尔值。请阅读关于 布尔值以获取更多信息。使用 === 运算符测试 这个函数的返回值。

      看这个例子:

      $foundKey = array_search(12345, $myArray);
      if(!isset($foundKey)){
          // If $myArray is null, then $foundKey will be null too.
          // Do something when both $myArray and $foundKey are null.
      } elseif ($foundKey===false) {
          // $myArray is not null, but 12345 was not found in the $myArray array.
      }else{
          // 12345 was found in the $myArray array.
      }
      

      【讨论】:

        【解决方案5】:

        来自文档:

        在haystack中搜索needle,如果在数组中找到则返回key,否则返回FALSE。

        【讨论】:

          【解决方案6】:

          如果没有找到,array_search 将返回 FALSE。如果它确实找到了针,它将返回针的数组键。

          更多信息请访问:http://php.net/manual/en/function.array-search.php

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-17
            • 2010-12-15
            • 1970-01-01
            • 2014-04-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-24
            相关资源
            最近更新 更多