how to get only not null element count in array php
$count = count(array_filter($array));
array_filter
will remove any entries that evaluate to false
, such as null
, the number 0
and empty strings. If you want only null
to be removed, you need:
$count = count(array_filter($array,create_function('$a','return $a !== null;')));
something like...
$count=0;foreach ($array as $k => $v){ if (!empty($v)) { $count++; }}
should do the trick.you could also wrap it in a function like:
function countArray($array){$count=0;foreach ($array as $k => $v){ if (!empty($v)) { $count++; }}return $count;}echo countArray($array);