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
+5.6k Swift : Get substring with rangeOfString() function example
+4.4k Linux/MacOSX : Search and delete files by extension
+9.4k Golang : How to get username from email address
+7.2k Golang : Check if one string(rune) is permutation of another string(rune)
+15.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+13.9k Golang : Get current time
+25.4k Golang : Convert long hexadecimal with strconv.ParseUint example
+14.6k Golang : Missing Bazaar command
+9k Golang : Capture text return from exec function example
+11.3k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+4.3k Javascript : How to show different content with noscript?
+9.9k Golang : Function wrapper that takes arguments and return result example