在PHP中,你如何改变一个数组元素的关键? [英] In PHP, how do you change the key of an array element?

查看:163
本文介绍了在PHP中,你如何改变一个数组元素的关键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格键= GT的关联数组;值其中密钥是一个数值,不过这不是一个连续的数值。密钥实际上是一个ID号和值是一个计数。这是对大多数情况下,但是我想,获取数组的人类可读的名称和使用,对于键的功能,在不改变的值。

我没有看到执行此功能,但我假设我需要提供旧键和新的密钥(这两者我有)和变换的阵列。是否有这样做的有效途径?


解决方案

  $改编[$则newkey] = $改编[$ oldkey];
未设置($改编[$ oldkey]);

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 ID number and the value is a count. This is fine for most instances, however I want a function that gets the human-readable name of the array and uses that for the key, without changing the value.

I didn't see a function that does this, but I'm assuming I need to provide the old key and new key (both of which I have) and transform the array. Is there an efficient way of doing this?

解决方案

$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);

这篇关于在PHP中,你如何改变一个数组元素的关键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆