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?

2
  • 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. Commented Apr 13, 2011 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] => ) Commented Apr 13, 2011 at 13:07

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

@Vinay it seems empty() is better anyway
but empty gives me that array has values
@Vinay what PHP version you're using?
in local machine php 5 and in server its php 4
array_filter is really handy! never used it before today, but it solves my problem! thank you!
11

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.