Goal: retrieve an element of data from within a PHP object by number.
This is the print_r($data) of the object:
stdClass Object (  => stdClass Object ( [TheKey] => 1456 [ThingName] => Malibu [ThingID] => 7037 [MemberOf] => California [ListID] => 7035 [UserID] => 157 [UserName] => John Doe ) )
I can't figure out how to pull a value out of it. This is only one record of a multi-record object that should be by id rather than a name.
These are some failed attempts to illustrate what the goal is:
echo $data -> 0 -> UserName; echo $data -> UserName;
Normally, PHP variable names can't start with a digit. You can't access
$dataas an array either as
stdClassdoes not implement
ArrayAccess— it's just a normal base class.
However, in cases like this you can try accessing the object attribute by its numeric name like so:
The only reason I can think of why Spudley's answer would cause an error is because you're running PHP 4, which doesn't support using
foreachto iterate objects.