PHP : How to check if an array is empty ?
In PHP, there are many times when I need to check if the return result array is empty or not. One way of achieving this is to use the empty()
function. However, there are times that the supposedly "empty" array is populated by 0 or false and it will cause the empty()
to evaluate the array inaccurately.
To ensure that the array is really empty before evaluation. Use the array_filter()
function.
<?php
$arr = array();
$arr = array_filter($arr);
if (!empty($arr)) {
echo "array is not empty";
}
else
{
echo "empty array";
}
?>
Hope this short tutorial is helpful.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+23.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+20.7k Golang : How to get time zone and load different time zone?
+16.8k Golang : Get future or past hours, minutes or seconds
+6k Grep : How to grep for strings inside binary data
+13.8k Golang : Recombine chunked files example
+38.6k Golang : How to iterate over a []string(array)
+5.1k PHP : Convert CSV to JSON with YQL example
+22.1k Golang : simulate tail -f or read last line from log file example
+6.1k Golang : How to determine if request or crawl is from Google robots
+8.5k Golang : Handle sub domain with Gin
+7.4k Golang : Reverse a string with unicode