Skip to content

Instantly share code, notes, and snippets.

@Snaver
Created December 12, 2013 00:19
Show Gist options
  • Save Snaver/7921096 to your computer and use it in GitHub Desktop.
Save Snaver/7921096 to your computer and use it in GitHub Desktop.
PHP functions for removing and adding array key string prefixes
/**
* Remove array key prefixes
*
*/
function remove_prefix($array, $prefix)
{
return array_combine(
array_map(
function($k,$prefix){
return preg_replace("/^$prefix/", '', $k);
},
array_keys($array),
array_fill(0 , count($array) , $prefix)
),
$array
);
}
/**
* Add array key prefixes
*
*/
function add_prefix($array, $prefix)
{
return array_combine(
array_map(
function($k,$prefix){
return $prefix.$k;
},
array_keys($array),
array_fill(0 , count($array) , $prefix)
),
$array
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment