2011-03-07 73 views
1

如何更改像我數組的鍵:如何更改數組鍵

$arrData = array('key1'=>'data1', 'key2'=>'data2') 

到:

$arrData = array('newKey1'=>'data1', 'newKey2'=>'data2') 

我喜歡做withing單一的語句,如:

$arrData = changeKey(array('newKey1'=>'data1', 'newKey2'=>'data2')) 
+3

重複http://stackoverflow.com/questions/240660/ – 2011-03-07 06:58:26

+0

創建一個簡單的函數 – 2011-03-07 07:02:23

+0

[This](http://stackoverflow.com/questions/240660/)可能會有所幫助。 – grc 2011-03-07 06:57:46

回答

2
$arrData['newKey1'] = $arrData['key1']; 
unset($arrData['key1']); 

[UPDATE]:

function changeKey($array, $newkey, $oldkey) 
{ 
     $array[$newkey] = $arrData[$oldkey]; 
     unset($array[$oldkey]); 
     return $array; 
} 

$array = changeKey($array, 'key1', 'key2');