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
+5k Golang : Measure execution time for a function
+37.9k Golang : How to iterate over a []string(array)
+9.9k Golang : Removes punctuation or defined delimiter from the user's input
+11.5k Golang : zlib compress file example
+6.9k Golang : Check from web if Go application is running or not
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+27.8k Golang : How to create new XML file ?
+10.6k Golang : Change date format to yyyy-mm-dd
+24.7k Mac/Linux and Golang : Fix bind: address already in use error
+16.5k Golang : Read data from config file and assign to variables
+5.6k Golang : Embedded or data bundling example
+9.5k Golang : How to delete element(data) from map ?