我想知道是否有内置或更好的方法来测试数组的所有元素是否为空。
这是我的(工作)解决方案:
<?php
function cr_isnull($data_array){
foreach($data_array as $value){
if(!is_null($value)){return false;}
}
return true;
}
?>
说明:
我不能使用empty()因为我的empty定义不适合PHP的定义。
有什么想法,或者我能跟我拥有的一样好吗?
答案 0 :(得分:4)
count(array_filter($myarray,'is_null')) == count($myarray);
OR
array_reduce($myarray,
function($result,$value) {
return $result && is_null($value);
},
TRUE
);