如何在PHP中传递一个额外的参数来array_map回调?
如何将单个附加参数传递给回调?在我的示例中,我想将(作为第二个参数,在 当前元素之后)传递给带有闭包的函数:array_map
$smsPattern
$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 应该是一个数组。
当然,di 原因是重用另一个正则表达式(如 .谢谢。$getLimit
$smsPattern