http://php.net/manual/en/function.in-array.php - 有问题,因为它仅通过 ==
检查。这里有没有聪明的 oneliner 可以通过 ===
来完成?
如果数组为空,即有 0 个元素,或者当数组不包含任何恰好为 null 的值时,则返回 false。如果数组至少有一个 === null
元素则为真。
最佳答案
in_array(null, $haystack, true);
如果您阅读 the doc you referenced ,您会看到该函数采用可选的第三个参数:
If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.
这是函数签名,具体如文档中所示:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Searches haystack for needle using loose comparison unless strict is set.
关于php - 如何在不循环的情况下检查数组是否具有 === null 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6556126/