确定PHP数组中的所有值是否都为null [英] Determine if all the values in a PHP array are null

查看:965
本文介绍了确定PHP数组中的所有值是否都为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找对性能最友好的方法,以检查数组中的所有值是否为null或它是否至少具有一个其他类型的元素.

I'm looking for the most performance friendly approach to check if all values in an array are null or if it at least has one element with other types.

即我需要一个名为containsOnlyNull($ array)的方法,该方法根据传递的数组返回布尔值

i.e. i need a method called containsOnlyNull($array), which returns booleans according to the passed array

例如:

$a = containsOnlyNull([null,null,null,null,null]);
$b = containsOnlyNull([null,null,1,null,null]);

// $a will be true
// $b will be false

任何建议将不胜感激:)

Any advice will be appreciated :)

推荐答案

function containsOnlyNull($input)
{
    return empty(array_filter($input, function ($a) { return $a !== null;}));
}

这篇关于确定PHP数组中的所有值是否都为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆