function drupal_map_assoc

7.x common.inc drupal_map_assoc($array, $function = NULL)
6.x common.inc drupal_map_assoc($array, $function = NULL)

Form an associative array from a linear array.

This function 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 input array. The values will be the same as the keys unless a function is specified, in which case the output of the function is used for the values instead.

Parameters

$array: A linear array.

$function: A name of a function to apply to all values before output.

Return value

An associative array.

25 calls to drupal_map_assoc()
aggregator_admin_settings in drupal-6.x/modules/aggregator/aggregator.admin.inc
Form builder; Configure the aggregator system.
aggregator_block in drupal-6.x/modules/aggregator/aggregator.module
Implementation of hook_block().
aggregator_form_feed in drupal-6.x/modules/aggregator/aggregator.admin.inc
Form builder; Generate a form to add/edit feed sources.
contact_admin_settings in drupal-6.x/modules/contact/contact.admin.inc
dblog_admin_settings in drupal-6.x/modules/dblog/dblog.admin.inc
dblog module settings form.

... See full list

File

drupal-6.x/includes/common.inc, line 1692
Common functions that many Drupal modules will need to reference.

Code

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;
  }
}