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
+8.2k Golang : Put UTF8 text on OpenCV video capture image frame
+5.4k Golang : How to profile or log time spend on execution?
+36.2k Golang : How to check if a string contains another sub-string?
+14.4k Golang : Get local IP and MAC address
+3.1k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+1.9k Golang : How to iterate a slice without using for loop?
+8.7k Golang : How to shuffle elements in array or slice?
+4.7k Golang : Find and draw contours with OpenCV example
+2.4k Unix/Linux : How to archive and compress entire directory ?
+3.9k Golang : Error reading timestamp with GORM or SQL driver
+7.5k Golang : delete and modify XML file content
+3.5k Golang : Create and shuffle deck of cards example