从数组中的COUNT中排除null值


Exclude null values from COUNT in array

如何从数组的计数中排除null值?因为count在计数中总是包含null值!

count(array_filter($array, function($x) {return !is_null($x); })
function count_nonnull($a) {
    $total = 0;
    foreach ($a as $elt) {
        if (!is_null($elt)) {
            $total++;
        }
    }
    return $total;
}

尝试使用foreach循环。

foreach($array as $index=>$value) {
    if($value === null) unset($array[$index]);
}
echo count($array);

或者,如果您不想修改阵列:

function myCount($arr)  {
    $count = 0;
    foreach($arr as $index=>$value) {
        if($value !== null) $count++;
    }
    return $count;    
}
echo myCount($array);

//最简单的方法
回波计数(array_filter($array));指令