如何将单个附加参数传递给array_map
回调?在我的示例中,我想将$smsPattern
(作为第二个参数,在当前元素 in 之后$featureNames
)传递给array_map
带有$getLimit
闭包的函数:
$features = $usage->getSubscription()->getUser()->getRoles();
// SMS regular expression in the form of ROLE_SEND_SMS_X
$smsPattern = '/^ROLE_SEND_SMS_(?P<l>\d+)$/i';
// Function to get roles names and X from a role name
$getNames = function($r) { return trim($r->getRole()); };
$getLimit = function($name, $pattern) {
if(preg_match($pattern, $name, $m)) return $m['l'];
};
// Get roles names and their limits ignoring null values with array_filter
$featuresNames = array_map($getNames, $features);
$smsLimits = array_filter(array_map($getLimit, $featureNames, $smsPattern));
使用此代码,我收到了一个奇怪的警告:
警告:array_map() [function.array-map]:参数 #3 应该是一个数组。
当然,原因是为了将$getLimit
闭包与另一个正则表达式(如$smsPattern
. 谢谢。