8

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?

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
n92
  • 6,776
  • 26
  • 87
  • 127
  • What do you mean by "array values is empty"? Useful functions/constructs: `is_array`, `count`, `empty`, `isset`. It all depends on what *exactly* you're trying to test. – Alin Purcaru Apr 13 '11 at 13:02
  • here is an array with no values only keys .i used count and empty but those functions are saying array has values .Array ( [delegate_title] => [delegate_firstname] => [delegate_lastname] => [delegate_jobtitle] => [delegate_email] => [delegate_phone] => [is_bringing_own_laptop] => ) – n92 Apr 13 '11 at 13:07

2 Answers2

21

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
}
Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
11

You want the empty() function, here's the documentation of the empty function http://php.net/manual/en/function.empty.php

mark-cs
  • 4,579
  • 21
  • 31