PHP: get a single key from object

后端 未结 3 1713
日久生厌
日久生厌 2021-02-15 05:40

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?

3条回答
  •  离开以前
    2021-02-15 06:11

    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);
    

提交回复
热议问题