Skip to content
Advertisement

How to get object id from array using php mongod

I have a array $document after query findOne()

Array
(
    [timestamp] => 1624382717
    [_id] => stdClass Object
        (
            [$id] => 60d2f531a5476f4eb220402b
        )
)

I want get 60d2f531a5476f4eb220402b by use $document['_id']->__toString(); but it never work

Advertisement

Answer

The stdClass object does not have any methods by default, so you address the $id field like this

echo $document['_id']->{'$id'};
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement