PHP: get a single key from object

后端 未结 3 1761
轮回少年
轮回少年 2021-02-15 05:58

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:29

    $array = array("foo" => "bar");
    
    $keys = array_keys($array);
    
    echo $keys[0];
    
    // Output: foo
    

    See: http://php.net/manual/en/function.array-keys.php

提交回复
热议问题