diff options
author | Derek Jones <derek.jones@ellislab.com> | 2010-10-07 16:38:55 +0200 |
---|---|---|
committer | Derek Jones <derek.jones@ellislab.com> | 2010-10-07 16:38:55 +0200 |
commit | 3a082fd3d6cb2e72612d51b9d8e54e93effb93eb (patch) | |
tree | 45cf986cc40cc781cbe9e2a0eae65d9b363078b9 /system | |
parent | aa7d3f9c04c165f6514aff58596ad7cff89ebe65 (diff) |
added elements() to the Array Helper to return elements from an array with specified keys only. Differs from array_intersect_assoc() in that a default value can be provided for keys that do not exist in the supplied array
Diffstat (limited to 'system')
-rw-r--r-- | system/helpers/array_helper.php | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index bd30b7c16..447ee1aa4 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -73,6 +73,46 @@ if ( ! function_exists('random_element')) } } +// -------------------------------------------------------------------- + +/** + * Elements + * + * Returns only the array items specified. Will return a default value if + * it is not set. + * + * @access public + * @param array + * @param array + * @param mixed + * @return mixed depends on what the array contains + */ +if ( ! function_exists('elements')) +{ + function elements($items, $array, $default = FALSE) + { + $return = array(); + + if ( ! is_array($items)) + { + $items = array($items); + } + + foreach ($items as $item) + { + if (isset($array[$item])) + { + $return[$item] = $array[$item]; + } + else + { + $return[$item] = $default; + } + } + + return $return; + } +} /* End of file array_helper.php */ /* Location: ./system/helpers/array_helper.php */
\ No newline at end of file |