In PHP, how do you change the key of an array element?

后端 未结 23 2720
逝去的感伤
逝去的感伤 2020-11-22 03:45

I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an I

23条回答
  •  庸人自扰
    2020-11-22 04:43

    The answer from KernelM is nice, but in order to avoid the issue raised by Greg in the comment (conflicting keys), using a new array would be safer

    $newarr[$newkey] = $oldarr[$oldkey];
    $oldarr=$newarr;
    unset($newarr);
    

提交回复
热议问题