How to Check all values of an array are equal or not in PHP?

Given an array with some elements, the task is to check whether all values of array elements are equal or not.

Below are the approaches to check if all values of an array are equal or not in PHP:

Table of Content

  • Using array_unique() Function
  • Using array_reduce() Function
  • Using array_filter() Function
  • Using a Loop

Using array_unique() Function

The array_unique() function removes duplicate values from an array. If all values in the array are equal, the resulting array after applying array_unique() will have only one element.

Example: This example shows the use of the above-mentioned appraoch.

PHP
<?php

function isAllEqual($arr) {
    return count(array_unique($arr)) === 1;
}

// Driver code
$arr1 = [1, 1, 1, 1];
$arr2 = [4, 2, 3, 4];

echo isAllEqual($arr1) 
    ? "All Values are Equal\n" 
    : "Values are Not Equal\n";

echo isAllEqual($arr2) 
    ? "All Values are Equal" 
    : "Values are Not Equal";

?>

Output
All Values are Equal
Values are Not Equal

Explanation:

  • array_unique($array) returns an array with duplicate values removed.
  • count(array_unique($array)) returns the number of unique values.
  • If the count is 1, all values in the original array are equal.

Using array_reduce() Function

The array_reduce() function iteratively reduces the array to a single value using a callback function.

Example: This example shows the use of the above-mentioned appraoch.

PHP
<?php

function isAllEqual($arr) {
    $val = reset($arr);
    return array_reduce($arr, 
        function($carry, $item) use ($val) {
            return $carry && ($item === $val);
        }, true);
}

// Driver code
$arr1 = [1, 1, 1, 1];
$arr2 = [4, 2, 3, 4];

echo isAllEqual($arr1) 
    ? "All values are equal\n" 
    : "Values are not equal\n";

echo isAllEqual($arr2) 
    ? "All values are equal." 
    : "Values are not equal.";

?>

Output
All values are equal
Values are not equal.

Explanation:

  • reset($array) gets the first value of the array.
  • array_reduce($array, $callback, $initial) applies the callback to each value, starting with the initial value.
  • The callback checks if the current item is equal to the first value.

Using array_filter() Function

The array_filter() function filters elements of an array using a callback function. If all elements pass the filter (i.e., are equal to the first element), the filtered array will have the same number of elements as the original array.

Example: This example shows the use of the above-mentioned appraoch.

PHP
<?php

function isAllEqual($arr) {
    $val = reset($arr);
    
    return count(array_filter($arr, 
        function($item) use ($val) {
            return $item === $val;
        })) === count($arr);
}

// Driver code
$arr1 = [1, 1, 1, 1];
$arr2 = [4, 2, 3, 4];

echo isAllEqual($arr1) 
    ? "All values are equal\n" 
    : "Values are not equal\n";

echo isAllEqual($arr2) 
    ? "All values are equal." 
    : "Values are not equal.";

?>

Output
All values are equal
Values are not equal.

Explanation:

  • reset($array) gets the first value of the array.
  • array_filter($array, $callback) returns an array of elements that pass the filter.
  • The callback checks if each element is equal to the first value.
  • The filtered array should have the same length as the original if all elements are equal.

Using a Loop

A simple loop can iterate through the array to check if all values are equal.

Example: This example shows the use of the above-mentioned appraoch.

PHP
<?php

function isAllEqual($arr) {
    $firstValue = reset($arr);
    foreach ($arr as $value) {
        if ($value !== $firstValue) {
            return false;
        }
    }
    return true;
}

// Driver code
$arr1 = [1, 1, 1, 1];
$arr2 = [4, 2, 3, 4];

echo isAllEqual($arr1) 
    ? "All values are equal\n" 
    : "Values are not equal\n";

echo isAllEqual($arr2) 
    ? "All values are equal." 
    : "Values are not equal.";

?>

Output
All values are equal
Values are not equal.

Explanation:

  • reset($array) gets the first value of the array.
  • A foreach loop iterates through each element.
  • If any element is not equal to the first value, the function returns false.
  • If the loop completes without finding any unequal elements, the function returns true.