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
+12.5k Golang : Pass database connection to function called from another package and HTTP Handler
+9k Golang : Write multiple lines or divide string into multiple lines
+11.9k Golang : Perform sanity checks on filename example
+5.8k Golang : Generate multiplication table from an integer example
+12.9k Golang : Convert(cast) int to int64
+18.8k Golang : Check whether a network interface is up on your machine
+35.2k Golang : Integer is between a range
+7.2k Android Studio : How to detect camera, activate and capture example
+13.2k Golang : Count number of runes in string
+5.2k Golang : fmt.Println prints out empty data from struct
+10.3k Golang : Select region of interest with mouse click and crop from image