Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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............................

like image 327
Kanishka Panamaldeniya Avatar asked Oct 06 '11 09:10

Kanishka Panamaldeniya


People also ask

How do you remove Blank values from an array?

In order to remove empty elements from an array, filter() method is used. This method will return a new array with the elements that pass the condition of the callback function.

How do I count the number of elements in an array PHP?

The count() function returns the number of elements in an array.

IS NOT NULL in PHP?

The is_null() function checks whether a variable is NULL or not. This function returns true (1) if the variable is NULL, otherwise it returns false/nothing.

How does count () work in PHP?

PHP count() function is an in-built function available in PHP, which counts and returns the number of elements in an array. It also counts the number of properties in an object. The count() function may return 0 for the variable, which has been declared with an empty array or for the variable which is not set.


2 Answers

simply use array_filter() without callback

print_r(array_filter($entry));
like image 148
Mr Coder Avatar answered Sep 22 '22 18:09

Mr Coder


$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;')));
like image 20
Niet the Dark Absol Avatar answered Sep 23 '22 18:09

Niet the Dark Absol