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
+16.8k Golang : Convert seconds to human readable time format example
+6.4k Golang : Error reading timestamp with GORM or SQL driver
+17k Golang : Determine if directory is empty with os.File.Readdir() function
+4.1k Golang : Stop goroutine without channel
+5.8k Golang : Dealing with struct's private part
+6.6k Prevent Write failed: Broken pipe problem during ssh session with screen command
+8.6k Golang : Allow Cross-Origin Resource Sharing request
+9.3k Use systeminfo to find out installed Windows Hotfix(s) or updates
+7.6k Golang : Temperatures conversion example
+3.3k Golang : Experimental Jawi programming language
+5.8k Golang : Getting Echo framework StartAutoTLS to work
+13.6k Golang : How do I get the local IP (non-loopback) address ?