drupal_map_assoc

Definition

drupal_map_assoc($array, $function = NULL)
includes/common.inc, line 1519

Description

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.

@result An associative array.

Parameters

$array A linear array.

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

Code

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

Drupal is a registered trademark of Dries Buytaert.