Variation from Laykes solution, just without creating a new array:
foreach($array as $key => $val) {
$array["..$key.."] = $val;
unset($array[$key]);
}
I assume you want to do this because you are replacing placeholders in your template with something like this:
$template = str_replace(array_keys($person), $person, $template);
If so, keep in mind that you are iterating twice over $person then. One time to change the keys and another time to get the keys. So it would be more efficient to replace the call to array_keys()
with a function that return the keys as padded values, e.g. something like
function array_keys_padded(array $array, $padding) {
$keys = array();
while($key = key($array)) {
$keys[] = $padding . $key . $padding;
next($array);
}
return $keys;
}
// Usage
$template = str_replace(array_keys_padded($person, '::'), $person, $template);
But then again, you could just as well do it with a simple iteration:
foreach($person as $key => $val) {
str_replace("::$key::", $val, $template);
}
But disregard this answer if you are not doing it this way :)
Out of curiosity, how are your users actually providing the array?