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
+6.6k Golang : Encrypt and decrypt data with x509 crypto
+31.9k Golang : Unmarshal JSON from http response
+7.7k Golang : Find file size(disk usage) with filepath.Walk
+1.7k Golang : How to check if a string with spaces in between is numeric?
+35.9k Golang : How to convert character to ASCII and back
+5.5k Golang : Query string with space symbol %20 in between
+6.5k Golang : How to make a file read only and set it to writable again?
+5.4k Golang : Listen and Serve on sub domain example
+55.6k Golang : How to return HTTP status code?
+6.2k Golang : reCAPTCHA example
+16.3k Golang : Convert(cast) string to rune and back to string example
+6.3k Golang : How to detect a server/machine network interface capabilities?