diff options
author | Christian Schneider | 2017-01-25 17:42:54 +0100 |
---|---|---|
committer | Christian Schneider | 2017-01-25 17:42:54 +0100 |
commit | fa73882a1221136f502b7c5a8a57a91fa7d2542a (patch) | |
tree | 04964b3a126bde89a23adf1a153a1ded090027a7 /it.class | |
parent | 3ebbf18ecd027fbeaa53166af4f6ceb6ca3e86a3 (diff) | |
download | itools-fa73882a1221136f502b7c5a8a57a91fa7d2542a.tar.gz itools-fa73882a1221136f502b7c5a8a57a91fa7d2542a.tar.bz2 itools-fa73882a1221136f502b7c5a8a57a91fa7d2542a.zip |
Second version of it::map with keys: Handle 'null', '1' for code and Traversable objects for array
Diffstat (limited to 'it.class')
-rw-r--r-- | it.class | 9 |
1 files changed, 7 insertions, 2 deletions
@@ -950,15 +950,20 @@ 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) && is_callable($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 = is_array($array) ? $array : iterator_to_array($array); + foreach (isset($p['keys']) ? it::filter_keys($array, $p['keys']) : $array as $k => $v) $result[$k] = $func($k, $v); return (array)$result; |