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
+7.8k Golang : Add build version and other information in executables
+19.4k Golang : How to run your code only once with sync.Once object
+9.7k Golang : Use regular expression to get all upper case or lower case characters example
+20.3k PHP : Convert(cast) int to double/float
+6.8k Golang : Get environment variable
+6.5k Swift : substringWithRange() function example
+5.9k Golang : Calculate US Dollar Index (DXY)
+5k Golang : Generate Interleaved 2 inch by 5 inch barcode
+18.9k Golang : Fix cannot download, $GOPATH not set error
+19.3k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader