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
+16k Golang : How to reverse elements order in map ?
+36.5k Golang : Validate IP address
+7k Golang : constant 20013 overflows byte error message
+12k Golang : Find and draw contours with OpenCV example
+10.1k Golang : Compare files modify date example
+5.5k Clean up Visual Studio For Mac installation failed disk full problem
+6.8k Golang : Join lines with certain suffix symbol example
+5.2k Golang : Calculate half life decay example
+14.9k Golang : Submit web forms without browser by http.PostForm example
+13.6k Golang : Get user input until a command or receive a word to stop
+19.1k Golang : Check whether a network interface is up on your machine
+20.2k Golang : Reset or rewind io.Reader or io.Writer