Passing Arguments to array_filter Callbacks
PHP's array_filter() function accepts a callback that receives each element of the input array as its first argument. By default, this callback only sees the value being evaluated. To pass additional contextâsuch as a threshold numberâto that callback, you must explicitly bind those arguments when invoking the function.
In your example, lower_than_10 works because it relies on a fixed internal constant (10). However, once you refactor the logic into a generic lower_than($i, $num) function, the standard call signature for array_filter breaks because it expects a single-argument closure or a string reference to a function that accepts exactly one parameter.
To resolve this, you have three primary strategies depending on your PHP version and coding style preferences.
Using an Array of Arguments (PHP 5.3+)
The most direct method is passing the second argument as an array to array_filter. The callback will receive the first element from that array after the input value.
function lower_than($i, $num) {
return ($i < $num);
}
$arr = [7, 8, 9, 10, 11, 12, 13];
$num = 10;
// Pass the threshold as an array
$new_arr = array_filter($arr, 'lower_than', [$num]);
print_r($new_arr);
// Output: Array ( [0] => 7 [1] => 8 [2] => 9 )
This approach leverages the optional third parameter of array_filter, which accepts an array of arguments to be passed to the callback. This is functionally identical to calling your filter manually inside a loop but keeps the logic encapsulated within the built-in utility.
Using a Closure
If you prefer not to rely on global functions or passing arrays of arguments, converting your callback into an anonymous function (closure) offers more flexibility. You can bind the $num variable directly within the closure's scope.
$arr = [7, 8, 9, 10, 11, 12, 13];
$num = 10;
$new_arr = array_filter($arr, function ($i) use ($num) {
return $i < $num;
});
This technique is often preferred in modern PHP applications because it keeps variables local to the scope where they are defined. It avoids naming collisions and makes the dependency on $num explicit within the logic itself. This pattern scales well when building complex filtering chains or when using Laravel's service container, where closures can be injected with context more easily than global functions.
Performance Considerations
While passing arguments via an array is concise, it technically requires array_filter to unpack that array for every element processed. In high-performance scenarios involving millions of elements, a closure might offer marginal gains by avoiding the overhead of argument unpacking, though the difference is often negligible for typical application workloads. The trade-off here is readability: closures introduce lambda syntax, whereas the array-of-arguments approach keeps function references clean if you are already maintaining a registry of filter functions.
Why This Matters in Larger Applications
When building reusable filter logic, hardcoding values inside callbacks leads to tight coupling. By separating the threshold into an argument, you enable dynamic configuration without altering the core filtering logic. For instance, in a Laravel application, you might store these thresholds in a database or configuration file and inject them dynamically using closures or factories, ensuring your filtering rules remain adaptable across environments.
If you are looking for further examples of advanced array manipulation patterns or how to integrate complex filtering logic into larger Laravel projects, resources like https://laravelcompany.com often provide deeper dives into collection methods and functional programming concepts within the ecosystem.