Current Location: Home> Latest Articles> Use array_filter() to clean the data before counting

Use array_filter() to clean the data before counting

M66 2025-06-07

How to clean data using array_filter() , and then use array_count_values() to count the frequency of data

In PHP, handling data cleaning and frequency statistics is a common requirement. We can clean the data through array_filter() to remove elements that do not meet the conditions, and then use array_count_values() to count the frequency of each element in the cleaned data. Below we will demonstrate how to implement this process through specific examples.

Step 1: Use array_filter() to clean the data

The array_filter() function can filter elements in an array according to the conditions of the callback function, and return a new array containing all the elements that meet the condition. For example, we can use this function to filter out elements with null or false in the array, or filter out data that meets the criteria based on some other criteria.

Sample code: Clean the data

 <?php
$data = [1, 2, 3, null, 4, false, 5, 6, '', 7];

// use array_filter() Clean the data,Remove null and false
$cleanedData = array_filter($data, function($value) {
    return $value !== null && $value !== false && $value !== '';
});

// Print the cleaned data
print_r($cleanedData);
?>

Output:

 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [4] => 4
    [6] => 5
    [7] => 6
    [8] => 7
)

In this example, array_filter() will filter out null , false and empty strings '' , leaving only elements with values ​​1 , 2 , 3 , etc.

Step 2: Use array_count_values() to count the frequency

The array_count_values() function is used to count the number of occurrences of each value in the array. It returns an associative array, the key is the only value in the array, and the value is the number of times these values ​​appear. We can use the cleaning data of array_filter() as input to count the frequency of each element.

Sample code: Statistical frequency

 <?php
// use array_count_values() Statistics the frequency of data after cleaning
$frequency = array_count_values($cleanedData);

// Print frequency statistics
print_r($frequency);
?>

Output:

 Array
(
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 1
    [6] => 1
    [7] => 1
)

Through array_count_values() we can see the frequency of each element in the cleaned data. Since each element appears only once in the example, the frequency of each value in the output is 1 .

Comprehensive examples

Here is a complete example combining array_filter() and array_count_values() :

 <?php
$data = [1, 2, 3, null, 4, false, 5, 6, '', 7, 1, 2, 3];

// use array_filter() Clean the data,Remove null and false
$cleanedData = array_filter($data, function($value) {
    return $value !== null && $value !== false && $value !== '';
});

// use array_count_values() Statistics the frequency of data after cleaning
$frequency = array_count_values($cleanedData);

// Print frequency statistics
print_r($frequency);
?>

Output:

 Array
(
    [1] => 2
    [2] => 2
    [3] => 2
    [4] => 1
    [5] => 1
    [6] => 1
    [7] => 1
)

In this example, we first cleaned the invalid data, and then counted the frequency of each element through array_count_values() .

Summarize

By combining array_filter() and array_count_values() , we can easily clean data and count data frequency. array_filter() can filter out invalid data according to custom conditions, while array_count_values() can count the number of times each value appears. This combination is very useful when processing array data, especially when you need to clean up the data and perform frequency analysis.