17

Check if all elements in Array are zero in PHP

 2 years ago
source link: https://thispointer.com/check-if-all-elements-in-array-are-zero-in-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Check if all elements in Array are zero in PHP

This tutorial will discuss about a unique way to check if all elements in array are zero in php.

We are going to use the array_reduce() function for this solution.

The array_reduce() function accepts three arguments,
1. An array
2. A callback function.
3. A boolean value. It will be returned if the array is empty.

It then applies the given callback function on all the elements of array, and reduce them to a single value.

The callbcak accepts two argument. First argument is the cumalative result value, which was returned by the previous call to this function. Second argument is the array the element. Inside the callback function we will check if the given element is equal to zero or not. It returns true, if the element is zero. Now array_reduce() function will apply this callback function on all the array elements. If all the elements in array are zero, then it will return true. If there is any element that is not zero, then it will return false.

Advertisements

If the array is empty, then it will return the boolean value sent as third argument.

Let’s see the complete example,

Copy to clipboard
<?php
$arr = [0, 0, 0, 0, 0, 0];
// Check if all elements in the array are zero
$isAllZero = array_reduce($arr,
function($result, $elem) {
return $result && ($elem === 0);
}, true);
if ($isAllZero) {
echo "Yes, all elements in array are zero";
} else {
echo "No, all elements in array are not zero";
?>
<?php
$arr = [0, 0, 0, 0, 0, 0];

// Check if all elements in the array are zero
$isAllZero = array_reduce($arr,
                          function($result, $elem) {
                              return $result && ($elem === 0);
                          }, true);

if ($isAllZero) {
  echo "Yes, all elements in array are zero";
} else {
  echo "No, all elements in array are not zero";
}
?>

Output

Copy to clipboard
Yes, all elements in array are zero
Yes, all elements in array are zero

Summary

We learned how to check if all elements of array are zero in PHP.

</div


Recommend

  • 14

    This tutorial will discuss about unique ways to check if all elements in a numpy array are unique. Table Of Contents Technique 1: Using numpy.unique() The numpy

  • 6

    This tutorial will discuss about unique ways to check if all elements in a numpy array are equal to value. Table Of Contents Technique 1: Using all() method We can use the...

  • 12

    This tutorial will discuss about unique ways to check if all elements in numpy array are false. Table Of Contents Technique 1: Using numpy.all() method We can use the...

  • 8

    Check if all elements in Array are true in PHP This tutorial will discuss about a unique way to check if all elements in array are true in php. To check if all elements in an array are true, we can use the

  • 8

    This tutorial will discuss about unique ways to check if all elements in array are unique in php. Table Of Contents Method 1: Using array_unique() function The

  • 12

    Check if all elements in array are null in PHP This tutorial will discuss about a unique way to check if all elements in array are null. We can use the array_reduce() function to get the work done h...

  • 19

    This tutorial will discuss about unique ways to check if all elements in array are equal in php. Table Of Contents Method 1: Using array_unique() function The

  • 10

    Check if All elements in Array are true in C++ This tutorial will discuss about a unique way to check if all elements in array are true in C++. To check if all elements of array are true, we are going to use the...

  • 9

    Check if All elements in Array are Zero in C++ This tutorial will discuss about a unique way to check if all elements in array are zero in C++. To check if all elements of array are zero, we are going to use the...

  • 6

    Check if All elements in Array are equal to value in C++ This tutorial will discuss about a unique way to check if all elements in array are equal to value in C++. To check if all elements of array matches with...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK