PHP Print keys from an object?

前端 未结 5 926
眼角桃花
眼角桃花 2021-01-07 18:07

I have an object BIRD and then there is [0] through [10] and each number has a subheading like \"bug\" or \"beetle\" or \"gnat\" and a value for each of those.

I wan

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-07 18:21

    If the 'object' is actually an associative array rather than a true object then array_keys() will give you what you need without warnings or errors.

    On the other hand, if your object is a true object, then you will get a warning if you try use array_keys() directly.

    You can extract the key-value pairs from an object as an associative array with get_object_vars(), you can then get the keys from this with array_keys():

    $keysFromObject = array_keys(get_object_vars($anObject));
    

提交回复
热议问题