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.9k Golang : GMail API create and send draft with simple upload attachment example
+6k Golang : Auto-generate reply email with text/template package
+6.8k Golang : Accept any number of function arguments with three dots(...)
+3.7k Golang : Trim everything onward after a word
+5.8k SSL : How to check if current certificate is sha1 or sha2 from command line
+13.8k Golang : Put UTF8 text on OpenCV video capture image frame
+27k error: trying to remove "yum", which is protected
+5.1k Golang : How to stop user from directly running an executable file?
+4.2k Golang : Selection sort example
+5.5k Golang : Sort words with first uppercase letter
+7.3k Golang : Identifying Golang HTTP client request