How to count non-empty entries in a PHP array?
You can use array_filter to only keep the values that are “truthy” in the array, like this:
array_filter($array);
If you explicitly want only non-empty
, or if your filter function is more complex:
array_filter($array, function($x) { return !empty($x); });# function(){} only works in in php >5.3, otherwise use create_function
So, to count only non-empty items, the same way as if you called empty(item)
on each of them:
count(array_filter($array, function($x) { return !empty($x); }));
Possible Solution: First you need to remove empty/null, false and zero values from an array and then count remaining values of an array
If you no need to remove zero values from an array, but remove null and false values
count(array_filter($arrayName, 'strlen'));//"strlen" use as second parameter if you no need to remove zero '0' values
if you need to remove zero, null and false values from an array
count(array_filter($arrayName));