views:

323

answers:

6

Is there any function available in PHP to check whether an array is empty or how can I do this without using loop?

For example: $b = array('key1' => '', 'key2' => '', 'key3' => '', 'key4' => '');

How can I check array $b contains empty values without using a loop?

+3  A: 

Whether you use a loop or some array function, you're still looping through the array so keep it simple and just loop through the array:

function isEmpty($arr) {
  foreach ($arr as $k => $v) {
    if ($v) {
      return false;
    }
  }
  return true;
}

Depending on what you want to define as empty, you may want to only check for empty strings:

function isEmpty($arr) {
  foreach ($arr as $k => $v) {
    if ($v === '') {
      return false;
    }
  }
  return true;
}
cletus
A: 

if you want to check for empty strings '' you can use in_array

if(!in_array('', $array)) echo 'array doesn’t contain empty strings';
if(in_array('', $array)) echo 'array does contain at least one empty string';

you might also want to try array_filter with an empty callback method, and compare that to an empty array (or use empty()):

if(empty(array_filter($array))) echo 'array only contains values evaluating to false';
knittl
what was the downvote for? i edited my question to show better what i meant
knittl
+1  A: 

Simply put, no.

If you don't want to do a literal foreach/for/while, you can use array_walk.

Jon Ursenbach
+10  A: 

Simple:

function allEmpty($array)
{
    return empty(array_filter($array));
}

function someEmpty($array)
{
    if ($array !== array_filter($array))
    {
     return true;
    }

    return false;
}
Alix Axel
Why was this down voted?
nilamo
Why the hell did I get a -1 vote?
Alix Axel
I guess because technically it's still looping, but it might be arguably better than writing your own loop.
deceze
+1 to counter-act. Downvoting without reason is stupid, guys.
nilamo
@deceze: No matter what, it's the best answer so far, at least IMHO.
Alix Axel
@eyze: I think so too. I actually posted the exact same thing, but removed it as duplicate.
deceze
Well, it IS still looping, and if your array happens to contain numbers it will conclude that all elements containing 0 are empty as well, which may be a source of error. I don't think this is the best answer so far, because it suggests that empty() does not use loops. Did not down vote it, though.
Banang
`empty()` does not use loops, `array_filter()` does. You can change your definition of "empty" by supplying a custom comparison function to `array_filter`.
deceze
@Banang: I can think of serialize() but it also loops. A solution for this will always loop, like you said before. But I guess we can agree that we don't have to take all **literally**, I think what paulrajj wants is a solution where **he** doesn't need to code the loop, unless I'm wrong.
Alix Axel
@paulrajj: That's because you didn't mentioned it was a multidimensional array, I'll patch up the code in a bit.
Alix Axel
thanks for the help.
paulrajj
@paulrajj: I can't come up with a solution for multidimensional arrays that doesn't uses loops (so little time!), try messing around with the code provided and array_map() or array_walk(). Sorry. =\
Alix Axel
For `someEmpty()`, why not just say `return $array !== array_filter($array);` and be done with it? Why create an unnecessary `if` block just to return a boolean value?
Chris Lutz
@Chris Lutz: Yeah, it's my coding style, but you can surely do that.
Alix Axel
It's a coding style that annoys the hell out of me, but fair enough.
Chris Lutz
@Chris: says the brainf*ck guy ;) lol
Alix Axel
+1  A: 

If that is the specific array you want to check (ie: it only has key=>values and empty is always key=>'') ...

$b = array('key1' => '', 'key2' => '', 'key3' => '', 'key4' => '');
$temp = array_flip($b);
if(count($temp) === 1 && empty($temp[0])){
    echo 'empty';
}

Otherwise you are going to have to use a loop... sorry.

SeanJA
A: 

I usually check it by using following condition:

if(is_array($array) and count($array)) {
 //  ... code here ...
}

That's all.

Tom Schaefer