1

My array looks like this

Array ( [] => ) 

To me it is empty, and I am looking for a way to check that.

First thought:

if( empty($array) ){
    echo 'Array is empty!';
}

To the empty-function, my array is not empty. A 'real' empty array looks like this: Array().

Second thought:

if( $array[''] == '' ){
    echo 'Array is empty!';
}

This is true for my empty array, but throws an error with any other array that does not contain such an empty key-value pair.

Any ideas?

Var_dump of my array:

array(1) { [""]=> NULL } 
6
  • 2
    Can you post a var_dump() of your actual array? Commented Oct 31, 2013 at 18:26
  • 2
    Your array isn't empty. It contains one element. That element (and its key) just happen to be the blank string (or NULL). Commented Oct 31, 2013 at 18:27
  • 1
    If you want that array to be considered empty, you should first prune these empty entries then check. Commented Oct 31, 2013 at 18:28
  • write var_dump($array); in your code: it will show you that you are wrong: your array looks like Array([] => NULL) Commented Oct 31, 2013 at 18:28
  • I do understand that my array is not REALLY empty. I just don't know how to check that. Commented Oct 31, 2013 at 18:32

3 Answers 3

6

Quick workaround for you:

$array = array_filter($array);

if (empty($array)) {
   echo 'Array is empty!';
}

array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false

EDIT 1:

In case you want to keep 0 you must use callback function. It will iterate over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array.

function customElements($callback){
  return ($callback !== NULL && $callback !== FALSE && $callback !== '');
}
$array = array_filter($array, "customElements");
var_dump($array);

Usage:

$array = array_filter($array, "customElements");

if (empty($array)) {
   echo 'Array is empty!';
}

It will keep 0 now, just what you were asking about.

EDIT 2:

As it was offered by Amal Murali, you could also avoid using function name in the callback and directly declare it:

$array = array_filter($array, function($callback) { 
   return ($callback !== NULL && $callback !== FALSE && $callback !== ''); 
});
Sign up to request clarification or add additional context in comments.

4 Comments

@eevaa: You can use a custom callback with array_filter(). See this answer or check my answer for another example :)
@AmalMurali Yes, thanks you! I know. Though, it could be easier to have a function in case of multiple scenarios.
Just a note for future vistiors: you can also make use of anonymous functions for a custom callback: $array = array_filter($array, function($elem) { return ($callback !== NULL && $callback !== FALSE && $callback !== ''); }) . Good answer +1 :)
@Thank you, Amal, I will add it to my answer, if you don't mind!
1

I don't know why you would want to have that array or what problem code generated it, but:

if(empty(array_filter($array))) {
    echo 'Array is empty!';
}

Obvious check:

if(empty($array) || (count($array) == 1 && isset($array['']) && empty($array['']))) {
    echo 'Array is empty!';
}

Comments

1

take a look at these calls:

 $a = Array(null => null);
 $x = (implode("", array_keys($a)));
 $y = (implode("", array_values($a)));

you can control data "emptyness" with them (keys, vals or both)

EDIT: this will return as empty:

$a = Array(null => false);

too, I guess this is a problem for you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.