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
+4.7k Golang : Grab news article text and use NLP to get each paragraph's sentences
+5.5k Nginx : Password protect a directory/folder
+46.2k Golang : How to convert JSON string to map and slice
+6.3k Android Studio : AlertDialog to get user attention example
+7.1k Golang : Variadic function arguments sanity check example
+5.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+3.7k Mac OSX : Get disk partitions' size, type and name
+7.9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+16.5k Golang : Simple client server example
+8.6k Golang : How to check if a website is served via HTTPS
+4.8k Javascript : How to replace HTML inside <div>?