0

I have a big associative array with over 1 000 items and I want to rename one key but the order must be preserved.

I don't wont to iterate over the complete array and copy it into a new.

hakre
  • 193,403
  • 52
  • 435
  • 836
mabe.berlin
  • 1,043
  • 7
  • 22

4 Answers4

1

have a look at the array_splice function: http://php.net/manual/en/function.array-splice.php

this will do the job

Fender
  • 3,055
  • 1
  • 17
  • 25
0
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
user85569
  • 394
  • 3
  • 10
0
  $array_keys = array_keys($array);

  $array_keys[array_search('old', $array_keys)] = 'new';

  $array = array_combine($array_keys, $array);
Michael
  • 11,912
  • 6
  • 49
  • 64
-2
$array[$newkeyname] = $array[$oldkeyname];
unset($array[$oldkeyname]);
bendataclear
  • 3,802
  • 3
  • 32
  • 51