Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if an array is empty in PHP?

Tags:

arrays

php

php4

I want to check that an array has no values or that the values in the array are empty. Can someone explain how to do this?

like image 444
n92 Avatar asked Apr 13 '11 13:04

n92


People also ask

How do you check if an array is empty?

To check if an array is empty or not, you can use the .length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not. An empty array will have 0 elements inside of it.

How check data is empty or not in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.

Is empty array empty PHP?

An empty array is falsey in PHP, so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).

Is empty array true in PHP?

An empty array evaluates to FALSE , any other array to TRUE (Demo):


1 Answers

Someday I've learned very smart solution here on SO

if(!array_filter($array)) {
  //array contains only empty values
}

or even smarter one (if applicable):

if(!array_filter($array,'trim')) {
  //array contains only empty values
}
like image 163
Your Common Sense Avatar answered Sep 24 '22 08:09

Your Common Sense