From b5c18e8a17024d4af8b2ea508d5ded2570893f07 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Thu, 12 Feb 2004 19:37:04 +0000 Subject: - Patch 5592 by Goba: introduced a new function, drupal_map_assoc(). --- includes/common.inc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'includes/common.inc') diff --git a/includes/common.inc b/includes/common.inc index 82c665179..8815458be 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1157,6 +1157,33 @@ function drupal_page_footer() { module_invoke_all("exit"); } +/** + * Walks through the provided array and constructs an associative + * array out of it. The keys of the resulting array will be the + * values of the passed array. The values will either be the same + * (if no function was specified), or the results of the function + * given the value. + * + * @param $array An array + * @param $function A name of a function to apply to all values + */ +function drupal_map_assoc($array, $function = NULL) { + if (!isset($function)) { + $result = array(); + foreach ($array as $value) { + $result[$value] = $value; + } + return $result; + } + elseif (function_exists($function)) { + $result = array(); + foreach($array as $value) { + $result[$value] = $function($value); + } + return $result; + } +} + /** * Wrapper around xml_parser_create() which extracts the encoding from the XML * data first and sets the output encoding to UTF-8. This function should be -- cgit v1.2.3