PHP - array_search()失败=== true,但没有开启!== false?

时间:2022-09-12 22:30:56

When I want to check if something is in the array and get the key back, I use the array_search() function.

当我想检查数组中是否存在某些内容并获取密钥时,我使用了array_search()函数。

Why is it when I compare the function to be exactly equal to true (=== true) it returns false, and when I compare it to not be exactly equal to false (!== false) it returns true?

为什么当我比较函数完全等于true(=== true)时它返回false,当我将它与不完全等于false(!== false)时它返回true?

<?php
    if(array_search($value, $array) === true)
    {
        // Fails
    }

    if(array_search($value, $array) !== false)
    {
        // Succeeds
    }
?>

Thanks in advance.

提前致谢。

4 个解决方案

#1


8  

array_search returns you needle when a match is found. it returns false only when match is not found. This is why only the opposite works in your case.

找到匹配项时,array_search会返回指针。只有在找不到匹配时才返回false。这就是为什么在你的情况下只有相反的工作原因。

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

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

#2


0  

It will fail because if the call is succesfull it returns the key no true.

它将失败,因为如果调用成功,则返回键不为true。

false is returned if it isnt found so === false is ok

如果找不到,则返回false = = false false

from the manual:

从手册:

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

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

#3


0  

array_search() does not return true.

array_search()不返回true。

If will only return false, if it can't find anything, otherwise it will return the key of the matched element.

如果只返回false,如果找不到任何内容,否则返回匹配元素的键。

According to the manual

根据手册

array_search — Searches the array for a given value and returns the corresponding key if successful ....

array_search - 在数组中搜索给定值,如果成功则返回相应的键....

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

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

#4


0  

Late to the party, but wanted to add some context / examples:

晚到派对,但想添加一些上下文/示例:

array_search will return the key (if the value is found) - which could be 0 - and will return FALSE if the value is not found. It never returns TRUE.

array_search将返回密钥(如果找到值) - 可能为0 - 如果未找到该值,则返回FALSE。它永远不会返回TRUE。

This code may sum it up better:

这段代码可以更好地总结:

// test array...
$array = [
    0   => 'First Item',
    1   => 'Second Item',
    'x' => 'Associative Item'
];

// example results:
$key = array_search( 'First Item', $array );       // returns 0
$key = array_search( 'Second Item', $array );      // returns 1
$key = array_search( 'Associative Item', $array ); // returns 'x'
$key = array_search( 'Third Item', $array );       // returns FALSE

Since 0 is a falsey value, you wouldn't want to do something like if ( ! array_search(...) ) {... because it would fail on 0 index items.

因为0是假值,所以你不想做if(!array_search(...)){...因为它会在0索引项上失败。

Therefore, the way to use it is something like:

因此,使用它的方式如下:

$key = array_search( 'Third Item', $array ); // returns FALSE
if ( FALSE !== $key ) {
    // item was found, key is in $index, do something here...
}

It's worth mentioning this is also true of functions like strpos and stripos, so it's a good pattern to get in the habit of following.

值得一提的是,strpos和stripos等功能也是如此,所以这是一个养成跟随习惯的好模式。

#1


8  

array_search returns you needle when a match is found. it returns false only when match is not found. This is why only the opposite works in your case.

找到匹配项时,array_search会返回指针。只有在找不到匹配时才返回false。这就是为什么在你的情况下只有相反的工作原因。

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

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

#2


0  

It will fail because if the call is succesfull it returns the key no true.

它将失败,因为如果调用成功,则返回键不为true。

false is returned if it isnt found so === false is ok

如果找不到,则返回false = = false false

from the manual:

从手册:

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

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

#3


0  

array_search() does not return true.

array_search()不返回true。

If will only return false, if it can't find anything, otherwise it will return the key of the matched element.

如果只返回false,如果找不到任何内容,否则返回匹配元素的键。

According to the manual

根据手册

array_search — Searches the array for a given value and returns the corresponding key if successful ....

array_search - 在数组中搜索给定值,如果成功则返回相应的键....

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

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

#4


0  

Late to the party, but wanted to add some context / examples:

晚到派对,但想添加一些上下文/示例:

array_search will return the key (if the value is found) - which could be 0 - and will return FALSE if the value is not found. It never returns TRUE.

array_search将返回密钥(如果找到值) - 可能为0 - 如果未找到该值,则返回FALSE。它永远不会返回TRUE。

This code may sum it up better:

这段代码可以更好地总结:

// test array...
$array = [
    0   => 'First Item',
    1   => 'Second Item',
    'x' => 'Associative Item'
];

// example results:
$key = array_search( 'First Item', $array );       // returns 0
$key = array_search( 'Second Item', $array );      // returns 1
$key = array_search( 'Associative Item', $array ); // returns 'x'
$key = array_search( 'Third Item', $array );       // returns FALSE

Since 0 is a falsey value, you wouldn't want to do something like if ( ! array_search(...) ) {... because it would fail on 0 index items.

因为0是假值,所以你不想做if(!array_search(...)){...因为它会在0索引项上失败。

Therefore, the way to use it is something like:

因此,使用它的方式如下:

$key = array_search( 'Third Item', $array ); // returns FALSE
if ( FALSE !== $key ) {
    // item was found, key is in $index, do something here...
}

It's worth mentioning this is also true of functions like strpos and stripos, so it's a good pattern to get in the habit of following.

值得一提的是,strpos和stripos等功能也是如此,所以这是一个养成跟随习惯的好模式。