Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Created January 19, 2011 17:29
Show Gist options
  • Save lukemorton/786497 to your computer and use it in GitHub Desktop.
Save lukemorton/786497 to your computer and use it in GitHub Desktop.
<?php
class Arr extends Kohana_Arr {
/**
* Recursive version of [array_map](http://php.net/array_map), applies the
* same callback to all elements in an array, including sub-arrays.
*
* // Apply "strip_tags" to every element in the array
* $array = Arr::map('strip_tags', $array);
*
* This customisation adds a third parameter allowing you to supply extra
* arguments to your callback function.
*
* $array = Arr::map('htmlentities', $array, array(NULL, 'utf-8'));
*
* [!!] Unlike `array_map`, this method requires a callback and will only map
* a single array.
*
* @param mixed callback applied to every element in the array
* @param array array to map
* @param array additional arguments
* @return array
*/
public static function map($callback, $array, array $args = array())
{
foreach ($array as $key => $val)
{
if (is_array($val))
{
$array[$key] = Arr::map($callback, $val, $args);
}
else
{
$array[$key] = call_user_func_array($callback, Arr::merge(array($val), $args));
}
}
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment