修改 PHP 对象属性名称
在 PHP 中是否可以更改对象属性键/名称?例如:
stdClass Object
(
[cpus] => 2
[created_at] => 2011-05-23T01:28:29-07:00
[memory] => 256
)
我希望将对象中的键 created_at
更改为 created
,留下一个如下所示的对象:
stdClass Object
(
[cpus] => 2
[created] => 2011-05-23T01:28:29-07:00
[memory] => 256
)
In PHP is it possible to change an Objects property key/name? For example:
stdClass Object
(
[cpus] => 2
[created_at] => 2011-05-23T01:28:29-07:00
[memory] => 256
)
I wish to change the key created_at
to created
in the Object leaving an object that looks like:
stdClass Object
(
[cpus] => 2
[created] => 2011-05-23T01:28:29-07:00
[memory] => 256
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不过,适配器类之类的东西可能是更可靠的选择,具体取决于需要执行此操作的位置和频率。
Something like an adapter class may be a more robust choice though, depending on where and how often this operation is necessary.
不,因为键是对值的引用,而不是值本身。
您最好复制原件,然后将其删除。
No, since the key is a reference to the value, and not a value itself.
You're best off copying the original, then removing it.
它类似于 @deceze 适配器,但不需要创建额外的类
Its similar to @deceze adapter, but without the need to create an extra class