diff options
author | Nathan Gass | 2010-11-11 16:25:45 +0000 |
---|---|---|
committer | Nathan Gass | 2010-11-11 16:25:45 +0000 |
commit | c9dabbd271afb8ac85009cd220f22a20a4373490 (patch) | |
tree | ef56e0c3619ec06e11b0219edbb6e135db76b708 /it.class | |
parent | 8874dd2a9b617c7f846fdbaf208e0c611f9aa23a (diff) | |
download | itools-c9dabbd271afb8ac85009cd220f22a20a4373490.tar.gz itools-c9dabbd271afb8ac85009cd220f22a20a4373490.tar.bz2 itools-c9dabbd271afb8ac85009cd220f22a20a4373490.zip |
add reorder argument and tests to it::filter_keys
Diffstat (limited to 'it.class')
-rw-r--r-- | it.class | 22 |
1 files changed, 16 insertions, 6 deletions
@@ -417,15 +417,25 @@ static function replace($replacements, $string, $p = array()) * Extract key => value pairs from assoc array by key * @param $array array to filter * @param $keys array or comma separated list of keys to keep + * @param $p['reorder'] reorder pairs to the order of the $keys array */ -static function filter_keys($array, $keys) +static function filter_keys($array, $keys, $p = array()) { $result = array(); - $keep = array_flip(is_string($keys) ? explode(",", $keys) : (array)$keys); - - foreach ($array as $key => $val) - if (isset($keep[$key])) - $result[$key] = $val; + if (is_string($keys)) + $keys = explode(",", $keys); + if ($p['reorder']) + { + foreach ((array)$keys as $key) + $result[$key] = $array[$key]; + } + else + { + $keep = array_flip((array)$keys); + foreach ($array as $key => $val) + if (isset($keep[$key])) + $result[$key] = $val; + } return $result; } |