PHP:从对象获取单个键 [英] PHP: get a single key from object

查看:74
本文介绍了PHP:从对象获取单个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单个键及其值的对象.但我不知道访问它的密钥.在不枚举对象的情况下获取密钥的最有效方法是什么?

I have an object with a single key and its value. But I don't know the key to access it. What is the most efficient way to get the key without enumerating the object?

推荐答案

如果你只是想访问值,你不需要key(实际上是属性名>) :

If you just want to access the value, you don't need the key (actually property name) at all:

$value = current((array)$object);

如果你真的想要属性名称,试试这个:

If you really want the property name, try this:

$key = key((array)$object);

这篇关于PHP:从对象获取单个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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