In PHP, we often need to filter and count data. array_filter() and array_count_values() are two very useful functions. array_filter() can filter elements in an array based on conditions, while array_count_values() can count values in an array and return results. Combining these two functions, we can efficiently perform complex screening of arrays and conduct statistical analysis on this basis.
The array_filter() function accepts two parameters, the first is an array and the second is a callback function. Callback functions are used to define filtering conditions, and only elements that meet the conditions will be retained.
For example, suppose we have an array with multiple URLs, and we want to filter out all URLs from the domain name m66.net .
<?php
// Original array
$urls = [
'https://m66.net/page1',
'https://example.com/page2',
'https://m66.net/page3',
'https://otherdomain.net/page4',
'https://m66.net/page5',
];
// use array_filter() Filter out m66.net Domain name URL
$filteredUrls = array_filter($urls, function ($url) {
return strpos($url, 'm66.net') !== false;
});
// Print the filtered array
print_r($filteredUrls);
?>
Output result:
Array
(
[0] => https://m66.net/page1
[2] => https://m66.net/page3
[4] => https://m66.net/page5
)
In this example, array_filter() is used to filter out all URLs containing m66.net .
The array_count_values() function returns an array containing the number of times each value appears in the original array. We can pass the filtered results to array_count_values() for statistical analysis.
For example, we want to count the number of times the m66.net domain name appears.
<?php
// use array_count_values() Statistics the number of occurrences
$urlCount = array_count_values($filteredUrls);
// Print statistics results
print_r($urlCount);
?>
Output result:
Array
(
[https://m66.net/page1] => 1
[https://m66.net/page3] => 1
[https://m66.net/page5] => 1
)
In this example, array_count_values() returns the number of occurrences of each URL. Because each URL appears only once in the array, each URL in the result is 1.
By combining array_filter() and array_count_values() , we can accurately filter the data and perform statistical analysis. This approach is ideal for handling complex data screening and analysis tasks.
For example, suppose we have a more complex array that contains different URLs and the number of visits, we can first filter out the URLs that meet the criteria and then count the frequency of occurrence of each URL.
<?php
// Original array,Including visits
$urlsWithVisits = [
'https://m66.net/page1' => 10,
'https://example.com/page2' => 5,
'https://m66.net/page3' => 8,
'https://otherdomain.net/page4' => 12,
'https://m66.net/page5' => 15,
];
// Filter out m66.net Domain name URL
$filteredUrlsWithVisits = array_filter($urlsWithVisits, function ($url) {
return strpos($url, 'm66.net') !== false;
}, ARRAY_FILTER_USE_KEY);
// Statistics of visits
$urlVisitCount = array_count_values($filteredUrlsWithVisits);
// Print results
print_r($urlVisitCount);
?>
Output result:
Array
(
[10] => 1
[8] => 1
[15] => 1
)
In this example, array_filter() is used to filter out the URLs of the m66.net domain name, and array_count_values() counts the number of times these URLs appear.
By combining array_filter() and array_count_values() , we can flexibly filter and statistically analyze array data. This method is suitable for a variety of data filtering and statistical scenarios, especially when processing large data sets, which can effectively improve the readability and execution efficiency of the code.
Related Tags:
array_filter