When dealing with arrays, it's quite trivial to use array_keys()
to grab the keys of your array. When dealing with objects, we have other functions like get_object_vars()
to handle those.
So if we have two simple variables, one array and one object:
+--------------------+---------------------------+
| Array | Object |
+--------------------+---------------------------+
| $array = array( | $object = new stdClass(); |
| 'foo' => 'bar', | |
| 'john' => 'doe', | $object->foo = 'bar'; |
| 'beep' => 'bop' | $object->john = 'doe'; |
| ); | $object->beep = 'bop'; |
+--------------------+---------------------------+
If we take the array_keys()
function and pass it the array, it does what we all expect and gives us an array with foo
, john
, and beep
:
Return: array(3) {
[0]=> string(3) "foo"
[1]=> string(4) "john"
[2]=> string(4) "beep"
}
And of course, as expected, if we instead pass the object to it, the function explodes and doesn't know what to do with it (if we really needed to, we could convert or even just typecast it to an array):
Return: NULL
WARNING array_keys() expects parameter 1 to be array, object given
But if we're dealing with a single key at a time, it seems to get a little bit more interesting. Using PHP's key()
function, we can extract whatever key we want.
key($array); //returns: string(3) "foo"
key($object); //returns: string(3) "foo"
// If we move the internal pointer
end( $array ); // key($array) returns: string(4) "beep"
end( $object ); // key($object) returns: string(4) "beep"
Looking at the documentation for the key()
function, should this not throw up a warning if an object
is used? I ask because I'm curious and was recently downvoted and told not to use key()
on objects. Generally I use other methods, but have used key()
in some instances without warnings (or errors) - and so I decided to look and nowhere does the documentation say anything about allowing an object
, just array &$array
. I suppose a similar thing could be said the the array pointer functions like end()
.