Is there a way to check if all the elements in an array are null in php

Go To StackoverFlow.com

1

Is there a way to check if all the elements in an array are null in PHP?

For instance, I have an array array(null,null,null,null) - is there a way to check this scenario?

I am looking for a better way than just looping through the entire array and checking each element.

2012-04-03 19:42
by Vishesh Joshi
Sure, loop through it with foreach and test each element - halfer 2012-04-03 19:44
I meant a better way to do it. As in some in built functio - Vishesh Joshi 2012-04-03 19:44
array_unique($array) == array(null) maybe. EDIT removed quote - Aaron W. 2012-04-03 19:45
How about checking if the data is empty before inserting it into the array in the first place? If it's empty, it's not added in the array. An array is useless if it has no conten - Kneel-Before-ZOD 2012-04-03 20:18
Any solution using array_unique() is O(n) in the best case, whereas a simple loop (and break) as @halfer suggests is O(n) only in the worst case. That means if you have a million items and the second item is not null, with array_unique() you'll still end up checking the remaining 999,998 items (not to mention potentially using quite a lot of memory) even though you already have your answer. Don't do this - Jordan Running 2012-04-03 20:26


7

Another simple way of making this work is to use the max() function.

max(array( 3, 4, null, null  ) )      # is 4
max(array( null, null, null, null)    # is null

Thus you can issue a simple if( is_null(max($array)) ) { ... } call.

2012-04-03 19:48
by pp19dd


2

Try this:

$nulls = array(null,null,null,null,null,null,null,null);
var_dump(array_unique($nulls) === array(null)); // prints true
2012-04-03 19:50
by anubhava


1

array_filter would work:

function checkIsset($val) {
    return isset($val);
}
$arr = array(null, null, null, ..., null);
$filteredArr = array_filter($arr, 'checkIsset');
if (count($filteredArr)) {
  //not all null
} else {
  //all null
}

or if (empty($filteredArr)) if you want the inverse.

2012-04-03 19:50
by zzzzBov
Is this tested - PeeHaa 2012-04-03 19:52
@RepWhoringPeeHaa, nope. it's more about the structure of how it would work - zzzzBov 2012-04-03 19:53
Could you please provide working code or add the fact that it is pseudocode - PeeHaa 2012-04-03 19:54
@RepWhoringPeeHaa, what's not working? Have you tried it out - zzzzBov 2012-04-03 19:54
I haven't tested it (then again I didn't try to answer), but I think you will get an error on isset as callback - PeeHaa 2012-04-03 19:55
@RepWhoringPeeHaa, i just checked, and you're right. Fixed it - zzzzBov 2012-04-03 19:57


-1

You can use array_filter()

like this:

$nulls = array(null,null,null,null,null);
if(count(array_filter($nulls)) == 0){
  echo "all null (or false)";
}

Dunno what you expect in this array, but this lumps false in with the nulls...

2012-04-03 19:46
by JKirchartz
I think you meant count($removenulls) == 0Aaron W. 2012-04-03 19:49
if you used array(0, '', null, array()); this would fail. array_filter simply checks for falsey values, not null values - zzzzBov 2012-04-03 19:52
My answer was up before I commented, then I noticed yours was similar - zzzzBov 2012-04-03 19:55
@JKirchartz have you considered testing some of you tries - PeeHaa 2012-04-03 19:58
Ads