How can i pass a single additional argument to array_map callback in PHP?
This is exactly what closures are about:
$getLimit = function($name) use ($smsPattern) {
if(preg_match($smsPattern, $name, $m)) return $m['l'];
};
$smsLimits = array_filter(array_map($getLimit, $features));
If you want to generalize it to other patterns, wrap the function creation into another function:
function patternMatcher($pattern) {
return function($name) use ($pattern) {
if(preg_match($pattern, $name, $m)) return $m['l'];
};
}
$getLimit = patternMatcher($smsPattern);
$smsLimits = array_filter(array_map($getLimit, $features));
And here it is wrapped up as an anonymous function:
$patternMatcher = function($pattern) {
return function($name) use ($pattern) {
if(preg_match($pattern, $name, $m)) return $m['l'];
};
};
$smsLimits = array_filter(array_map($patternMatcher($smsPattern), $features));