php - 如何在不循环的情况下检查数组是否具有 === null 的值?

标签 php

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/

相关文章:

php - 在 easyphp 上使用 PHP 导入和导出 MySQL 数据库

php - 如何使用 PHP 从完整路径中获取文件名?

php - 通过 laravel Mysql 使用 Distinct 和 group

php - 如何设置 php 代码以将 ssl 与 https 连接一起使用

php - 在 PHP 中使用 IMAP 知道电子邮件是否是回复

php - 从一个数据库读取数据并使用 PHP 插入另一个数据库时出现编码错误

javascript - 当我在 opencart 中加载时出现错误

php - 在独特的类别存档页面上隐藏主要类别标题

php - 未设置($_GET ['someVariable'])不起作用

php - WAMP include_path 错误