在多维数组中重命名数组键?

时间:2012-07-20 16:40:08

标签: php multidimensional-array

这就是我得到的......

 $shop = array( array( first_name => "john", 
                      last_name => "doe"
                    ));         
 foreach($shop as $key => $value) {
    foreach($value as $key2 => $value2) {
            $shop[$key][$key2] = $shop[$key]['newname'];
            unset($shop[$key][$key2]);
        }}

print_r($shop);

哪个输出

Array ( [0] => Array ( ) )

1 个答案:

答案 0 :(得分:0)

更改分配线(第6行)并使newname动态化。如下所示:

...foreach ($value as $key2 => $value2) {
    $newname = 'new_' . $key2;
    $shop[$key][$newname] = $shop[$key][$key2];
    unset($shop[$key][$key2]);
}...