'how to get only not null element count in array php

i want to get only not null values count in that array , if i use count() or sizeof it will get the null indexes also .

in my case

i have an array like this Array ( [0] => )

the count is 1 . but i want to get the not null count , inthis case it should be 0 , how can i do this , please help............................



Solution 1:[1]

$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;')));

Solution 2:[2]

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);

Solution 3:[3]

One option is

echo "Count is ".count(array_filter($array_with_nulls, 'strlen'));

If you don't count empty and nulls values you can do this

echo "Count is ".count(array_filter($array_with_nulls));

In this blog you can see a little more info

http://briancray.com/2009/04/25/remove-null-values-php-arrays/

Solution 4:[4]

// contact array
$contact_array = $_POST['arr'];

//remove empty values from array
$result_contact_array = array_filter($contact_array);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fat potato
Solution 2 totallyNotLizards
Solution 3 linkamp
Solution 4 Ehab Aboassy