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
+12.5k Golang : Listen and Serve on sub domain example
+7.1k Golang : Calculate how many weeks left to go in a given year
+15k Golang : How to get Unix file descriptor for console and file
+12.2k Golang : "https://" not allowed in import path
+25k Golang : Convert uint value to string type
+26.5k Golang : Find files by extension
+7.9k Golang : Tell color name with OpenCV example
+9.2k Golang : Scramble and unscramble text message by randomly replacing words
+16.6k Golang : How to generate QR codes?
+13.7k Golang : How to check if a file is hidden?
+8.8k Golang : Handle sub domain with Gin
+10.9k Golang : Web routing/multiplex example