How To Check if an array is empty in PHP?

You can use PHP inbuilt functions; array_filter() and empty() to check if an array is empty.

array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false. Otherwise in your particular case empty() construct will always return true if there is at least one element even with "empty" value.

empty() function checks to see whether the variable is empty, null, false, or a representation of zero. It doesn't return true just because the value associated with an array entry is false.

$errors = array_filter($_POST);

if (empty($errors)) {

 echo "array is empty.";
}
else
{
 echo "It is not empty.";
}
Ref: http://stackoverflow.com/a/8329005

Comments

  1. I like this post about array in php. It is very simple to get information about array through few line of codes. Web Development Company Islamabad

    ReplyDelete

Post a Comment

Popular Posts