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.5k Golang : Progress bar with ∎ character
+18.3k Golang : Example for RSA package functions
+15.9k Golang : Get sub string example
+23.3k Golang : Get ASCII code from a key press(cross-platform) example
+32.8k Golang : How to check if a date is within certain range?
+13.2k Golang : Date and Time formatting
+9.6k PHP : Get coordinates latitude/longitude from string
+10k Golang : Get login name from environment and prompt for password
+5.7k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+5.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+28.9k Golang : Get first few and last few characters from string
+8.4k PHP : How to parse ElasticSearch JSON ?