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
+8.1k Golang : Auto-generate reply email with text/template package
+6.7k Default cipher that OpenSSL used to encrypt a PEM file
+12.3k Golang : flag provided but not defined error
+14.2k Golang : How to convert a number to words
+8k Golang : Get final or effective URL with Request.URL example
+16.7k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+17.9k Golang : Convert IPv4 address to decimal number(base 10) or integer
+5.2k Golang : Reclaim memory occupied by make() example
+21.9k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+7k Golang : Get Alexa ranking data example
+39k Golang : How to read CSV file
+7.4k Golang : How to stop user from directly running an executable file?