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
+9.2k Golang : Scramble and unscramble text message by randomly replacing words
+16.2k Golang : Get IP addresses of a domain name
+5.1k Golang : How to deal with configuration data?
+8.7k Golang : Build and compile multiple source files
+18.2k Golang : Write file with io.WriteString
+22.4k Golang : Round float to precision example
+10.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+4.4k JavaScript : Rounding number to decimal formats to display currency
+14k Golang : Recombine chunked files example
+22.8k Golang : Read a file into an array or slice example
+11.1k Golang : Characters limiter example
+8.8k Golang : Capture text return from exec function example