-
-
Save mdrakibhossainhawlader/ad59e54ac9b7b9fde417694f979814e6 to your computer and use it in GitHub Desktop.
PHP: Array Map Associative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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