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.5k Golang : How to setup a disk space used monitoring service with Telegram bot
+4.5k Javascript : Get operating system and browser information
+21.5k Golang : Find biggest/largest number in array
+4.8k Unix/Linux : How to get own IP address ?
+3.9k Golang : Shortening import identifier
+6.4k Golang : Another camera capture GUI application with GTK and OpenCV
+16.9k Golang : Execute shell command
+7.6k Golang : Turn string or text file into slice example
+4.1k Golang : Launching your executable inside a console under Linux
+5.4k Nginx : How to block user agent ?
+17.3k Golang : Set or Add HTTP Request Headers
+6.2k Golang : Add build version and other information in executables