article

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.     }

Comments

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.)
2 quarters, 2 nickels, 1 penny + 100

Trackback URL

http://derocher.org/~brian//trackback.php?ParentId=14633

form