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

Forms 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.


$array: A linear array.

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

Return value

An associative array.

38 calls to drupal_map_assoc()
aggregator_block_configure in modules/aggregator/aggregator.module
Implements hook_block_configure().
aggregator_form_aggregator_admin_form_alter in modules/aggregator/aggregator.processor.inc
Implements hook_form_aggregator_admin_form_alter().
aggregator_form_feed in modules/aggregator/aggregator.admin.inc
Form constructor for adding and editing feed sources.
aggregator_form_opml in modules/aggregator/aggregator.admin.inc
Form constructor for importing feeds from OPML.
blog_block_configure in modules/blog/blog.module
Implements hook_block_configure().

... See full list


includes/common.inc, line 2822
Common functions that many Drupal modules will need to reference.


function drupal_map_assoc($array, $function = NULL) {
  // array_combine() fails with empty arrays:
  // http://bugs.php.net/bug.php?id=34857.
  $array = !empty($array) ? array_combine($array, $array) : array();
  if (is_callable($function)) {
    $array = array_map($function, $array);
  return $array;


Dane Powell’s picture

$fruit = array('apples', 'oranges');
// $fruit is now array('apples' => 'apples', 'oranges' => 'oranges')
Les Lim’s picture

So you would need

$fruit = drupal_map_assoc($fruit);
Chi’s picture

izus’s picture

Drupal 8 way is :
// From ConfigSingleExportForm.php
$names = array_combine($names, $names);

ferdly’s picture

For clarity, and for those looking for documentation, when a 'drupal specific' function is removed, and the replacement is a standard PHP function, is it not best to declare it the 'PHP way' or at least the 'Drupal 8 way using PHP'? No biggie :-)

Further, the Dupal 8 way using PHP for the second $function parameter in drupal_map_assoc($array,$function) is, as in the original, array_map().

However, you could use the very similar array_walk() to better effect as both value and key are implicit parameters, and you are able to supply an additional third parameter (which, since it is of type 'mixed' could conceivably be an array of many additional parameters).

Finally, by making the two steps separate the user can choose whether to array_walk()/array_map() either the $keys parameter or the $values parameter or both before combining.

sobi3ch’s picture

Anonymous function work either.

 drupal_map_assoc(array(1,2,3), function($element) {
   return $element. ' <<<';
// produce:
// array(
//   1 => '1 <<<',
//   2 => '2 <<<',
//   3 => '3 <<<'
// )