1

how do i change keys of my array like:

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

to:

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

I like it to be done withing a single stmt like:

$arrData = changeKey(array('newKey1'=>'data1', 'newKey2'=>'data2'))
3
  • This might help. Commented Mar 7, 2011 at 6:57
  • 3
    duplicate of stackoverflow.com/questions/240660 Commented Mar 7, 2011 at 6:58
  • create a simple function Commented Mar 7, 2011 at 7:02

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.