Last active
June 25, 2024 12:38
-
-
Save abiusx/4ed90007ca693802cc7a56446cfd9394 to your computer and use it in GitHub Desktop.
PHP's array_map, but instead of mapping values, maps keys.
This file contains 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 | |
/** | |
* Array map, but maps values to new keys instead of new values | |
* @return array same arrays with keys mapped | |
*/ | |
function array_map_key($callback,$array) | |
{ | |
$out=array_reduce($array, function ($carry,$val) use ($array,$callback){ | |
$key=call_user_func($callback,$val); | |
$carry[$key]=$val; | |
return $carry; | |
}); | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment