Skip to content

Instantly share code, notes, and snippets.

@abiusx
Last active August 25, 2023 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abiusx/4ed90007ca693802cc7a56446cfd9394 to your computer and use it in GitHub Desktop.
Save abiusx/4ed90007ca693802cc7a56446cfd9394 to your computer and use it in GitHub Desktop.
PHP's array_map, but instead of mapping values, maps keys.
<?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