How to Count PHP Array Avoiding Empty Value



Hello php devs in this quick example i will show you how to count array length without null value in php. That mean we will count non-empty entries in a PHP array using php array_filter() array function. 

Sometimes we need just those index which has value in a PHP array. Those index are null in a array, we will not count those index. So let' see how we can do it.

$array = [
    'test',
    'test2'
     null,
     null
];

$array = array_filter($array, function($x) { 
            return !empty($x);
        });

$count = sizeof($array);

 

Hope it can help you.

 

PHP
Facebook Github
A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using Laravel Vue and React Js Application.