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
+6.3k Golang : How to stop user from directly running an executable file?
+6.2k Golang : Trim everything onward after a word
+4.8k Golang : Error handling methods
+13.2k Golang : Reset buffer example
+7.6k Golang : Set or add headers for many or different handlers
+4.3k Golang : The Tao of importing package
+10.8k Golang : Determine if time variables have same calendar day
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+5.6k Golang : Spell checking with ispell example
+8.7k Golang : Sort and reverse sort a slice of floats
+23.2k Golang : GORM read from database example
+24.8k Golang : Convert(cast) string to uint8 type and back to string