Adding one character to each array key in PHP

Possible duplicate:
The fastest way to add a 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 the array, for example:

name => Brand age => 23 weight => 150

must be converted to:

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

Any help would be appreciated, thanks.

+3
source share
3 answers

Iterate the array, add a new element with the changed key and delete the original element:

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

Since it foreachworks with an internal copy of an array, you are not starting an infinite loop.

+14
source
+1

, $K1, $K2 $K1 = "r _". $K2, , Gumbo - .

, / . , , .

, , , PHP . , , .

+1

All Articles