Created
December 12, 2013 00:19
-
-
Save Snaver/7921096 to your computer and use it in GitHub Desktop.
PHP functions for removing and adding array key string prefixes
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
/** | |
* 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