0

In PHP, I can easily rename an associative array's key by doing the following.

$array = array(
    "tom" => "25",
    "bob" => "36",
);

$array["charlie"] = $array["tom"];
unset($array["tom"]);

However, this scrambles the order of the array. "bob" becomes the first element and "charlie" (which was "tom") is now the second element.

echo reset($array); // outputs 36 instead of 25

Is there a way to rename an array key without scrambling its order?

NOTE: I realize I can reconstruct the array, but that is really ugly. I would much rather avoid that.

gluxon
  • 750
  • 1
  • 7
  • 16

0 Answers0