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
+15.7k Golang : Get file permission
+8.4k Golang : Convert(cast) []byte to io.Reader type
+9.9k Golang : Random Rune generator
+16.1k Golang : Find out mime type from bytes in buffer
+20.5k PHP : Convert(cast) int to double/float
+9.1k Golang : Terminate-stay-resident or daemonize your program?
+18.2k Golang : Logging with logrus
+11.3k Golang : Display a text file line by line with line number example
+10.8k How to test Facebook App on localhost ?
+10.3k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+5.7k Golang : List all packages and search for certain package