Skip to content
Advertisement

Describe php object

I want to create a function that will do this: check the object and if the property 1 is 001 then print John else if the property 2 is 002 the print Jenny but I dont know how to read the property 1 [001] and describe it as a variable.

The result that I cURL from a file:

stdClass Object
(
    [id] => stdClass Object
        (
            [001] => John
            [002] => Jenny
         )
)

Advertisement

Answer

Check if the id is present in your object and if it is return the name. As an example:

function get_name_from_id($obj, $id) {
    return $obj->id->$id ?? false;
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement