How can i pass a single additional argument to array_map callback in PHP?

Posted by Gremo on Stack Overflow See other posts from Stack Overflow or by Gremo
Published on 2012-06-03T16:37:23Z Indexed on 2012/06/03 16:40 UTC
Read the original article Hit count: 198

Filed under:
|
|
|
|

How can i pass a single additional argument to array_map callback? In my example i'd like to pass $smsPattern (as a second argument, after current element in $featureNames) to the function array_map with $getLimit closure:

$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));

With this code i'm getting a weird warning:

Warning: array_map() [function.array-map]: Argument #3 should be an array.

Of course di reason is for reusing $getLimit closure with another regular expression like $smsPattern. Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays