PHP Print keys from an object? PHP Print keys from an object? arrays arrays

PHP Print keys from an object?


You could easily do it by type casting the object:

$keys = array_keys((array)$BIRD);


Similar to brenjt's response, this uses PHP's get_object_vars instead of type casting the object.

$array = get_object_vars($object);$properties = array_keys($array);


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