PHP: How to find the mode(s) of an array (4 examples)

Updated: February 19, 2024 By: Guest Contributor Post a comment

Introduction

Finding the mode of an array — the value(s) that appears most frequently — can be a common task in data processing and analytics with PHP. Unlike calculating the mean (average) or the median, finding the mode might seem straightforward but involves a bit more logic, especially when an array has multiple modes or none at all. This tutorial walks through different approaches to find the mode(s) of an array using PHP, from basic to advanced. By the end, you’ll have a solid understanding of how to efficiently solve this problem in various scenarios.

Basic Approach to Find a Single Mode

Let’s start with a simple approach to find a single mode of an array. This method works well when you’re only interested in one mode, even if the array has multiple modes. It returns the first mode it finds.

$array = [1, 2, 2, 3, 3, 3, 4];
$countValues = array_count_values($array);
arsort($countValues);
$mode = key($countValues);
echo "The mode is: $mode";

In this example, we first count how often each value appears in the array using array_count_values. Then, we sort the counts in descending order with arsort, so the most frequent value comes first. Finally, we use key to get the first key of the sorted array, which is the mode. The output for this array would be “3” since it appears the most times.

Finding All Modes

If your array might have multiple modes and you want to identify all of them, you’ll need a slightly different approach. Here’s how you can find all modes:

$array = [1, 2, 2, 3, 3, 3, 4, 4, 4];
$countValues = array_count_values($array);
$maxCount = max($countValues);
$modes = array_keys($countValues, $maxCount);
print_r($modes);

After counting the value frequencies, instead of sorting, we find the highest frequency using max. Then, we get all keys (array values) that match this highest count, effectively finding all modes. For the given array, the output will be: Array ( [0] => 3 [1] => 4 )

This result shows us that both 3 and 4 are modes as they appear with the highest frequency.

Effectively Handling Arrays with No Mode

A more complex scenario is handling arrays that have no mode; that is, every value appears with the same frequency. In such cases, depending on your application, you might want to return all values, no values, or perhaps a specific indicator. Here’s a technique to check if an array has a mode:

$array = [1, 2, 3, 4, 5];
$countValues = array_count_values($array);
if(max($countValues) > 1) {
    $maxCount = max($countValues);
    $modes = array_keys($countValues, $maxCount);
    print_r($modes);
} else {
    echo "No mode found";
}

In this snippet, we add a conditional check after calculating the value frequencies. We only proceed to find the modes if the highest frequency is greater than 1, indicating a mode exists. Otherwise, we output “No mode found”.

Using a Custom Function to Find Modes

To streamline the mode-finding process for repeated use, it’s practical to wrap the logic in a function. This section presents a custom PHP function to find all modes of an array or return a specified value if no mode exists:

function findModes($array) {
    $countValues = array_count_values($array);
    $maxCount = max($countValues);
    if($maxCount > 1) {
        return array_keys($countValues, $maxCount);
    }
    return "No mode or multiple modes with equal frequency";
}
$array = [1, 2, 2, 3, 3, 4];
print_r(findModes($array));

This function encapsulates the logic discussed earlier for checking the existence of a mode and finding all modes, efficiently handling both singular and multiple modes, including the scenario where no mode exists. It’s flexible and reusable for different array inputs.

Conclusion

Calculating the mode(s) of an array in PHP requires understanding how to count value frequencies and determine the most common values efficiently. While PHP doesn’t have a built-in function specifically for this task, the solutions provided here, from basic to advanced, equip you with the techniques needed to handle various scenarios, including arrays with single modes, multiple modes, and no mode. With practice, integrating these methods into your data processing scripts will become second nature.