During development, array operations are one of the common tasks. PHP provides many built-in functions to manipulate arrays, among which array_count_values() is a very practical function. Its function is to count the number of occurrences of all values in the array and return the result as an associative array. However, sometimes we want to encapsulate this operation into a custom function to reuse it in the project and simplify the development process.
This article will show how to encapsulate array_count_values() by creating a custom function to improve code reusability, as well as code simplicity and maintainability.
array_count_values() is a built-in function in PHP that can count the number of occurrences of all values in an array. It returns an associative array where the key is the value that appears in the array and the value is the number of occurrences of that value.
$array = ["apple", "banana", "apple", "orange", "banana", "apple"];
$count = array_count_values($array);
print_r($count);
Array
(
[apple] => 3
[banana] => 2
[orange] => 1
)
From the above example, we can see that array_count_values() can easily count the frequency of array elements.
Although array_count_values() is a very convenient built-in function, we can further encapsulate it to create a more versatile and extensible custom function. This way, we can reuse it when needed and centrally manage the function logic.
We will create a custom function countValues which takes an array as an argument, call array_count_values() to count the occurrences of each value in the array, and return the result.
/**
* Statistics the number of occurrences of each value in the array
*
* @param array $inputArray The input array
* @return array Returns the associative array of statistics results
*/
function countValues(array $inputArray): array {
// use array_count_values Make a count
return array_count_values($inputArray);
}
$array = ["apple", "banana", "apple", "orange", "banana", "apple"];
$result = countValues($array);
print_r($result);
Array
(
[apple] => 3
[banana] => 2
[orange] => 1
)
By encapsulation, we can manage and reuse this code more clearly. And, if we need to extend this logic in the future (such as adding control of case sensitivity, or filtering out certain specific values, etc.), we only need to modify the countValues() function.
The biggest advantage of encapsulating functions is that they improve the reusability of the code. No matter where you need to do similar statistics in the project, just call the countValues() function, and you don't need to write a piece of array_count_values() call code every time. In this way, the code becomes more modular and easy to manage.
During development, we often encounter similar tasks: counting the number of occurrences of certain values in an array. By encapsulating array_count_values() , we can directly call countValues() during development without paying attention to the underlying implementation details every time. This not only improves development efficiency, but also makes the code more readable.
If there are new requirements or modifications for the encapsulated function, we only need to modify one place (i.e. the countValues() function itself). This makes the code easier to maintain and can be flexibly adjusted according to your needs. For example, if we want to ignore certain specific values during statistics or filter values, the encapsulated function can be easily expanded.
To better demonstrate the scalability of the encapsulation, suppose we want to filter out certain specific values when counting values. We can modify the countValues() function and add an optional filtering parameter.
/**
* Statistics the number of occurrences of each value in the array,And filter out the specified value
*
* @param array $inputArray The input array
* @param array $excludeValues Values that need to be excluded
* @return array Returns the associative array of statistics results
*/
function countValues(array $inputArray, array $excludeValues = []): array {
// Filter out unnecessary values
$filteredArray = array_diff($inputArray, $excludeValues);
// use array_count_values Make a count
return array_count_values($filteredArray);
}
$array = ["apple", "banana", "apple", "orange", "banana", "apple"];
$exclude = ["banana"]; // Filter out 'banana'
$result = countValues($array, $exclude);
print_r($result);
Array
(
[apple] => 3
[orange] => 1
)
Through extensions, we can selectively exclude certain values when calling, making the function more flexible and practical.
By creating custom function encapsulation array_count_values() , we can achieve more efficient, concise and maintainable code. Encapsulation not only improves the reusability of the code, but also simplifies the development process and makes the code logic clearer. Whether it is in the basic statistical functions or when more functions need to be expanded, the encapsulated functions can be flexibly responded to and improve development efficiency.
I hope this article can help you understand how to simplify the development process by encapsulating built-in functions and effectively improve the maintainability and reusability of your code.