[PHP]:如果什么也没找到,array_search() 会返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2581619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:00:12  来源:igfitidea点击:

[PHP]: What does array_search() return if nothing was found?

phparrayssearch

提问by Derek Adair

What does array_search() return if nothing was found?

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

I have the need for the following logic:

我需要以下逻辑:

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

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

回答by Pascal MARTIN

Quoting the manual page of array_search():

引用手册页array_search()

Returns the key for needle if it is found in the array, FALSEotherwise.

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


Which means you have to use something like :


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

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

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

Note I used the !==operator, that does a type-sensitive comparison ; see Comparison Operators, Type Juggling, and Converting to booleanfor more details about that ;-)

注意我使用了!==运算符,它进行了类型敏感的比较;看到比较运算符类型戏法,并转换为布尔值,了解有关详情;-)

回答by user187291

if you're just checking if the value exists, in_arrayis the way to go.

如果你只是检查值是否存在,in_array是要走的路。

回答by Jaime Montoya

According to the official documentation at http://php.net/manual/en/function.array-search.php:

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

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

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

See this example:

看这个例子:

$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.
}

回答by oggy

From the docs:

从文档:

Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise.

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

回答by ashurexm

array_search will return FALSE if nothing is found. If it DOES find the needle it will return the array key for the needle.

如果未找到任何内容,array_search 将返回 FALSE。如果它确实找到了针,它将返回针的数组键。

More info at: http://php.net/manual/en/function.array-search.php

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

回答by Basj

One has to be careful to distinguish found, index 0and not found, for this use the !== falsetest. Example:

必须小心区分found, index 0not found,为此使用!== false测试。例子:

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$i = array_search('red', $array);
echo ($i !== false) ? $i : -1;  // 1

$i = array_search('blue', $array);
echo ($i !== false) ? $i : -1;  // 0

$i = array_search('blueee', $array);
echo ($i !== false) ? $i : -1;  // -1 i.e. not found
?>