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
+8.4k Golang : Get all countries currencies code in JSON format
+14.7k Golang : Generate universally unique identifier(UUID) example
+9.6k Golang : Bubble sort example
+28k Golang : Get time.Duration in year, month, week or day
+14.9k Golang : Get IP addresses of a domain name
+8.9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+3.6k Adding Skype actions such as call and chat into web page examples
+8.2k Golang : How to get ECDSA curve and parameters data?
+11.1k Golang : calculate elapsed run time
+8.2k Golang : Scramble and unscramble text message by randomly replacing words
+12.9k Golang : Google Drive API upload and rename example
+9.4k Golang : Simple File Server