Skip to content

Instantly share code, notes, and snippets.

@abiusx
Last active June 25, 2024 12:38
Show Gist options
  • 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