summaryrefslogtreecommitdiff
path: root/it.class
diff options
context:
space:
mode:
authorUrban Müller2017-01-25 16:05:28 +0100
committerUrban Müller2017-01-25 16:05:28 +0100
commitf59a46a6dfe0194ba43092304029b2b843ed54bc (patch)
tree1d5222d16101f27891408c8ce4e62726b2035199 /it.class
parentb0f1f6067066ac5c4216672181150431270611bc (diff)
downloaditools-f59a46a6dfe0194ba43092304029b2b843ed54bc.tar.gz
itools-f59a46a6dfe0194ba43092304029b2b843ed54bc.tar.bz2
itools-f59a46a6dfe0194ba43092304029b2b843ed54bc.zip
optional keys selection in it::map
Diffstat (limited to 'it.class')
-rw-r--r--it.class12
1 files changed, 8 insertions, 4 deletions
diff --git a/it.class b/it.class
index a9b6662..1731330 100644
--- a/it.class
+++ b/it.class
@@ -950,18 +950,22 @@ static function date($format = "", $stamp = null)
* Iterate over an array, replacing every element by function value or expression
* @param $code The function or expression to apply. Expression may contain $k for keys and $v for values
* @param $array The array to iterate over
+ * @param $p['keys'] Only modify elements with given keys (keys can be array or comma separated)
*/
-static function map($code, $array)
+static function map($code, $array, $p = null)
{
static $cache = array();
+ if (is_string($code) && it::match('^[\w:]+$', $code))
+ $code .= '($v)';
+
if (!is_callable($func = $code) && !($func = $cache[$code]))
$func = $cache[$code] = create_function('$k,$v', "return $code;");
- foreach ($array as $k => $v)
- $result[$k] = $func($k, $v);
+ foreach ($p['keys'] ? it::filter_keys($array, $p['keys']) : $array as $k => $v)
+ $array[$k] = $func($k, $v);
- return (array)$result;
+ return (array)$array;
}
/**