In PHP, the array_filter() function is used to filter elements in an array by a callback function. This means it filters the array through the given rule, leaving elements that match the rule. In some scenarios, we may need to flexibly adjust the filtering conditions according to different needs, so it becomes very important to parameterize the filtering rules so that they can be reused and adapted in different scenarios.
First, let's review the basic usage of array_filter() . Suppose we have a simple array and want to filter out even numbers in it:
<?php
$array = [1, 2, 3, 4, 5, 6];
$filtered = array_filter($array, function($item) {
return $item % 2 !== 0; // Only odd numbers are retained
});
print_r($filtered);
?>
Running results:
Array
(
[0] => 1
[2] => 3
[4] => 5
)
In this example, we define a filtering rule through the callback function - only odd numbers are retained. However, in practical applications, there may be multiple different filtering rules, and we want to be able to choose filtering rules flexibly instead of writing new callback functions every time.
In order to make the filtering rules more flexible, we can parameterize the filtering rules, so that different filter conditions can be selected according to the needs in different scenarios. The key to implementation is to pass the filtering rule as a parameter to array_filter() .
We can encapsulate filtering rules into functions or classes so that different rules are called in different scenarios.
<?php
function filterArray(array $array, callable $filterFunction): array {
return array_filter($array, $filterFunction);
}
// Define different filtering rules
$isOdd = function($item) {
return $item % 2 !== 0; // Only odd numbers are retained
};
$isEven = function($item) {
return $item % 2 === 0; // Only even numbers are retained
};
$array = [1, 2, 3, 4, 5, 6];
// Use different filtering rules
$filteredOdd = filterArray($array, $isOdd);
$filteredEven = filterArray($array, $isEven);
print_r($filteredOdd); // Only odd numbers are retained
print_r($filteredEven); // Only even numbers are retained
?>
Output result:
Array
(
[0] => 1
[2] => 3
[4] => 5
)
Array
(
[1] => 2
[3] => 4
[5] => 6
)
In the example above, we pass the filtering rules to filterArray() through function parameters, so that different rules can be flexibly selected in different scenarios. This method makes the code more concise and reusable.
Sometimes, we may want to dynamically decide the filtering rules through some external inputs (such as user settings, configuration files, etc.). In this case, we can generate corresponding filtering rules based on the passed parameters.
<?php
function generateFilterRule(string $type): callable {
if ($type === 'odd') {
return function($item) {
return $item % 2 !== 0; // Only odd numbers are retained
};
} elseif ($type === 'even') {
return function($item) {
return $item % 2 === 0; // Only even numbers are retained
};
} else {
return function($item) {
return true; // No filtering
};
}
}
$array = [1, 2, 3, 4, 5, 6];
$filterRule = generateFilterRule('odd'); // Select odd rule
$filtered = array_filter($array, $filterRule);
print_r($filtered);
?>
Output result:
Array
(
[0] => 1
[2] => 3
[4] => 5
)
In this example, the generateFilterRule() function determines the filtering rules based on the passed string, making the filtering conditions more flexible.
In actual development, filtering rules sometimes rely on external data sources (such as database query results, API return values, etc.). In this case, we can incorporate external data sources into the filtering rules. For example, we can dynamically generate filtering rules based on the configuration obtained from m66.net .
<?php
// Assume this is from m66.net Filter rule configuration obtained
$filterConfig = [
'type' => 'even' // The value obtained from the external interface here can be 'odd' or 'even'
];
// Generate filtering rules based on external data sources
$filterRule = generateFilterRule($filterConfig['type']);
$array = [1, 2, 3, 4, 5, 6];
$filtered = array_filter($array, $filterRule);
print_r($filtered);
?>
In this way, we can dynamically adjust the filtering rules according to the configuration of external interfaces such as m66.net , so that the code can adapt to different needs.
Related Tags:
array_filter