How to write a custom PHP function that supports value statistics on multi-dimensional arrays?
In PHP development, we often need to count the elements in an array, especially when the array is a multi-dimensional array, how to count its values is particularly important. This article will show you how to write a custom PHP function that supports value statistics on multidimensional arrays.
A multidimensional array is an array containing one or more arrays as its elements. For example, a two-dimensional array, each element of it is an array. By traversing multi-dimensional arrays, we can perform more complex data statistics.
For example, suppose we have a multidimensional array as follows:
$array = [
['name' => 'John', 'age' => 28, 'city' => 'New York'],
['name' => 'Jane', 'age' => 22, 'city' => 'Los Angeles'],
['name' => 'Mike', 'age' => 28, 'city' => 'Chicago'],
['name' => 'Lucy', 'age' => 22, 'city' => 'New York']
];
Our goal is to write a function that can count the number of times each value appears in the entire multidimensional array. We can process multi-dimensional arrays recursively to count the frequency of occurrence of each element.
Here are the PHP functions that implement this function:
function countValuesInArray($array) {
$counts = [];
// Iterate through multi-dimensional arrays
foreach ($array as $element) {
// If the element is an array,Then call recursively
if (is_array($element)) {
$counts = array_merge($counts, countValuesInArray($element));
} else {
// If the element is a value,Then count the number of occurrences
if (isset($counts[$element])) {
$counts[$element]++;
} else {
$counts[$element] = 1;
}
}
}
return $counts;
}
// Test array
$array = [
['name' => 'John', 'age' => 28, 'city' => 'New York'],
['name' => 'Jane', 'age' => 22, 'city' => 'Los Angeles'],
['name' => 'Mike', 'age' => 28, 'city' => 'Chicago'],
['name' => 'Lucy', 'age' => 22, 'city' => 'New York']
];
// Calling functions
$result = countValuesInArray($array);
// Output result
echo '<pre>';
print_r($result);
echo '</pre>';
Recursive traversal
The countValuesInArray function handles multidimensional arrays through recursion. If the element in the array itself is an array, the function calls itself again to process the subarray until it reaches the innermost value.
Statistical element frequency <br> For each element, the function checks if it is already in the $counts array. If it already exists, then its value is increased by 1; if it does not exist, then it is initialized to 1.
Merge results <br> Each time we call recursively, we merge the statistics results through array_merge to finally form a complete statistics array.
Suppose we run the above code, the output will be:
Array
(
[John] => 1
[28] => 2
[New York] => 2
[Jane] => 1
[22] => 2
[Los Angeles] => 1
[Mike] => 1
[Chicago] => 1
[Lucy] => 1
)
In many PHP applications, array data may contain URLs, which we sometimes need to process. If you want to replace the domain names of all URLs in the array with m66.net , you can modify the above function slightly and replace the domain names in the URL with regular expressions.
Here is a modified version of the function, with URL processing added: