Skip to content

Instantly share code, notes, and snippets.

@sepehr
Created September 1, 2012 02:56
Show Gist options
  • Select an option

  • Save sepehr/3563081 to your computer and use it in GitHub Desktop.

Select an option

Save sepehr/3563081 to your computer and use it in GitHub Desktop.
PHP: Array Map Associative
<?php
/**
* Converts a linear array to its associative equivalent.
*
* @param $array
* Linear array to process.
* @param $function
* Callback name to call against each value.
*
* @return array
* @see http://bugs.php.net/bug.php?id=34857
*/
function array_map_assoc(array $array, $function = NULL)
{
// array_combine() fails with empty arrays
empty($array) OR $array = array_combine($array, $array);
// Callback map
is_callable($function) AND $array = array_map($function, $array);
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment