Home » Php » php – Is there a method to check if all array items are '0'?

php – Is there a method to check if all array items are '0'?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have an array

$data = array( 'a'=>'0', 'b'=>'0', 'c'=>'0', 'd'=>'0' );

I want to check if all array values are zero.

if( all array values are '0' ) {

   echo "Got it";

} else {

  echo "No";
}

Thanks

How to&Answers:

I suppose you could use array_filter() to get an array of all items that are non-zero ; and use empty() on that resulting array, to determine if it’s empty or not.

For example, with your example array :

$data = array( 
       'a'=>'0',
       'b'=>'0',
       'c'=>'0',
       'd'=>'0' );

Using the following portion of code :

$tmp = array_filter($data);
var_dump($tmp);

Would show you an empty array, containing no non-zero element :

array(0) {
}

And using something like this :

if (empty($tmp)) {
    echo "All zeros!";
}

Would get you the following output :

All zeros!

On the other hand, with the following array :

$data = array( 
    'a'=>'0', 
    'b'=>'1', 
    'c'=>'0', 
    'd'=>'0' );

The $tmp array would contain :

array(1) {
  ["b"]=>
  string(1) "1"
}

And, as such, would not be empty.

Note that not passing a callback as second parameter to array_filter() will work because (quoting) :

If no callback is supplied, all entries of input equal to FALSE (see
converting to boolean) will be removed.

Answer:

How about:

// ditch the last argument to array_keys if you don't need strict equality
$allZeroes = count( $data ) == count( array_keys( $data, '0', true ) );

Answer:

Use this:

$all_zero = true;
foreach($data as $value)
    if($value != '0')
    {
        $all_zero = false;
        break;
    }
if($all_zero)
    echo "Got it";
else
    echo "No";

This is much faster (run time) than using array_filter as suggested in other answer.

Answer:

you can loop the array and exit on the first non-zero value (loops until non-zero, so pretty fast, when a non-zero value is at the beginning of the array):

function allZeroes($arr) {
  foreach($arr as $v) { if($v != 0) return false; }
  return true;
}

or, use array_sum (loops complete array once):

function allZeroes($arr) {
  return array_sum($arr) == 0;
}

@fireeyedboy had a very good point about summing: if negative values are involved, the result may very well be zero, even though the array consists of non-zero values

Answer:

Another way:

if(array_fill(0,count($data),'0') === array_values($data)){
    echo "All zeros";
}

Answer:

Another quick solution might be:

if (intval(emplode('',$array))) {
  // at least one non zero array item found
} else {
  // all zeros
}

Answer:

if (!array_filter($data)) {
   // empty (all values are 0, NULL or FALSE)
}
else {
   // not empty
}

Answer:

I’m a bit late to the party, but how about this:

$testdata = array_flip($data);
if(count($testdata) == 1 and !empty($testdata[0])){
    // must be all zeros
}

A similar trick uses array_unique().

Answer:

You can use this function

function all_zeros($array){//true if all elements are zeros
    $flag = true;
    foreach($array as $a){
        if($a != 0)
            $flag = false;
    }
    return $flag;
}

Answer:

You can use this one-liner: (Demo)

var_export(!(int)implode($array));

$array = [0, 0, 0, 0]; returns true

$array = [0, 0, 1, 0]; returns false

This is likely to perform very well because there is only one function call.

My solution uses no glue when imploding, then explicitly casts the generated string as an integer, then uses negation to evaluate 0 as true and non-zero as false. (Ordinarily, 0 evaluates as false and all other values evaluate to true.)

…but if I was doing this for work, I’d probably just use !array_filter($array)