<?php
$array = array('a' => 1, 'd' => 2, 'c' => 3); //associative array
// rename $array['d'] as $array['b']
$array = replace_key_function($array, 'd', 'b');
var_export($array); // array('a' => 1, 'b' => 2, 'c' => 3); same order!
function replace_key_function($array, $key1, $key2)
{
if ($index !== false) {
$keys[$index] = $key2;
}
return $array;
}
PD9waHAKCiRhcnJheSA9IGFycmF5KCdhJyA9PiAxLCAnZCcgPT4gMiwgJ2MnID0+IDMpOyAvL2Fzc29jaWF0aXZlIGFycmF5CgovLyByZW5hbWUgJGFycmF5WydkJ10gYXMgJGFycmF5WydiJ10KJGFycmF5ID0gcmVwbGFjZV9rZXlfZnVuY3Rpb24oJGFycmF5LCAnZCcsICdiJyk7Cgp2YXJfZXhwb3J0KCRhcnJheSk7IC8vIGFycmF5KCdhJyA9PiAxLCAnYicgPT4gMiwgJ2MnID0+IDMpOyBzYW1lIG9yZGVyIQoKZnVuY3Rpb24gcmVwbGFjZV9rZXlfZnVuY3Rpb24oJGFycmF5LCAka2V5MSwgJGtleTIpCnsKICAgICRrZXlzID0gYXJyYXlfa2V5cygkYXJyYXkpOwogICAgJGluZGV4ID0gYXJyYXlfc2VhcmNoKCRrZXkxLCAka2V5cyk7CiAgICAKICAgIGlmICgkaW5kZXggIT09IGZhbHNlKSB7CiAgICAgICAgJGtleXNbJGluZGV4XSA9ICRrZXkyOwogICAgICAgICRhcnJheSA9IGFycmF5X2NvbWJpbmUoJGtleXMsICRhcnJheSk7CiAgICB9CiAgICAKICAgIHJldHVybiAkYXJyYXk7Cn0=