Possible Duplicate:
Fastest way to add prefix to array keys?

I had a quick question about arrays in PHP. I need to add a couple of characters to each key in an array, for instance:

name => Mark age => 23 weight = > 150

needs to be converted to:

r_name => Mark r_age => 23 r_weight => 150

Any help would be appreciated, thanks.


duplicate of a lot of posts, in fact

Written by roirodriguez

Accepted Answer

Iterate the array, add a new item with the modified key and delete the original item:

foreach ($arr as $key => $val) {
    $arr['r_'.$key] = $val;

As foreach works with an internal copy of the array, you won’t run into an infinite loop.

Written by Gumbo
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki