PHP

How to check for empty values within a array using PHP

Written by Ian Carnaghan · 25 sec read >

If you have an array called $array with the below values

Array
(
    [0] => 1299016800
    [1] => 
    [2] => 
)

As we can see in the above example we have 2 empty values within the array, now how do we determine this using PHP

First we get the size of the array

$mainSize = sizeof($array);

Now we get the size of the array without the empty values

$emptySize =sizeof(array_filter($array));

Now we compare the 2 different sizes, if they are equal to each other, then it means that there aren’t empty values within the array, else there are empty values within the array

$mainSize = sizeof($array);
$emptySize =sizeof(array_filter($array));
 
if($mainSize == $emptySize){
              echo 'Array not empty';
}else{
              echo 'Array is empty';
}

Last Updated On March 22, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

avatar
  Subscribe  
Notify of