Open In App

Count Frequency of an Array Item in PHP

Last Updated : 31 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array containing some elements (array elements can be repeated), the task is to count the frequency of array items using PHP.

Below are the approaches to count the frequency of an array item in PHP:

Using array_count_values() Function

The array_count_values() function is a built-in PHP function that counts all the values of an array and returns an associative array where keys are the unique values from the input array and values are the counts.

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

PHP
<?php

// Declare an array
$arr = [10, 15, 20, 15, 20, 12, 10];

// Count the frequency of array items
$freq = array_count_values($arr);

print_r($freq);

?>

Output
Array
(
    [10] => 2
    [15] => 2
    [20] => 2
    [12] => 1
)

Explanation:

  • $array is the array containing the elements.
  • array_count_values($array) returns an associative array with the count of each element.

Using a Loop

You can use a loop to manually count the frequency of each element in the array. This approach is more flexible and can be customized to fit specific needs.

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

PHP
<?php

// Declare an array
$arr = [10, 15, 20, 15, 20, 12, 10];

// Initialize an empty array to 
// store the frequency
$freq = [];

// Iterate through the array
// to count the frequency
foreach ($arr as $value) {
    if (isset($freq[$value])) {
        $freq[$value]++;
    } else {
        $freq[$value] = 1;
    }
}

print_r($freq);

?>

Output
Array
(
    [10] => 2
    [15] => 2
    [20] => 2
    [12] => 1
)

Explanation:

  • $arr is the array containing the elements.
  • $freq is an associative array initialized to store the count of each element.
  • The foreach loop iterates through each element in the array.
  • If the element is already in $freq, increment its count; otherwise, set its count to 1.

Using array_reduce() Function

The array_reduce() function reduces the array to a single value using a callback function, making it a powerful tool for various array manipulations, including counting frequencies.

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

PHP
<?php

// Declare an array
$arr = [10, 15, 20, 15, 20, 12, 10];

// Use array_reduce() function to count
// the frequency of array items
$freq = array_reduce($arr, function($carry, $item) {
    if (isset($carry[$item])) {
        $carry[$item]++;
    } else {
        $carry[$item] = 1;
    }
    return $carry;
}, []);

print_r($freq);

?>

Output
Array
(
    [10] => 2
    [15] => 2
    [20] => 2
    [12] => 1
)

Explanation:

  • $arr is the array containing the elements.
  • array_reduce($arr, function($carry, $item) { ... }, []) reduces the array to an associative array where keys are the unique elements and values are their counts.

Using array_map() and array_unique() Functions

This approach uses the array_map() function to create an associative array where the keys are the unique elements of the array and the values are their respective counts. The array_unique() function is used to get unique elements from the array, and array_filter() is used to count occurrences.

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

PHP
<?php

$arr = [10, 15, 20, 15, 20, 12, 10];

$unique_elements = array_unique($arr);

$freq = array_map(function($item) use ($arr) {
    return count(array_filter($arr, function($v) use ($item) {
        return $v == $item;
    }));
}, array_flip($unique_elements));

print_r($freq);

?>

Output
Array
(
    [10] => 0
    [15] => 0
    [20] => 0
    [12] => 0
)


Explanation:

  1. $arr is the array containing the elements.
  2. array_unique($arr) returns the unique elements from the array.
  3. array_flip($unique_elements) flips the array keys and values, so the unique elements become the keys.
  4. array_map() iterates over the unique elements and uses array_filter() to count occurrences of each element in the original array.
  5. The resulting array $freq contains the counts of each unique element.


Next Article

Similar Reads