Action executed in 0.000

A variation on PHP array_map()

tags: PHP, array_map, scalar
Here's a variation on array_map() that: (1) passes the array key to the callback (2) passes a single scalar argument to the callback
  1. /**
  2. An array_map which takes scalar arguments.
  3. @param $cb is a callback.  It can be a simple function or an object method.
  4. @param $ara is an array to operate on.
  5. @param $args is a single argument to pass to the call back.  It can be an array.
  6. */
  7. function array_map_scalar( $cb, $ara, $args )
  8.     {
  9.     $range = array();
  10.     foreach ($ara as $k => $v)
  11.         {
  12.         $range[$k] = call_user_func( $cb, $k, $v, $args );
  13.         }
  14.     return $range;
  15.     }


none yet

Post a Comment

* indicates a required field
anonymous (If you want to identify yourself, please sign in first.)
required This field is required.

Max size is 2 MB, aspect ratio 3:4 width:height
required This field is required.
Please include a short description.
required This field is required.

480 characters remaining.
is public

(Use this field if you have to. 3000 characters remaining.)
1 dime, 1 penny, 1 nickel + 100

Trackback URL