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
)
)
My Answer:
In this scenario, the PHP object is a stdClass object that contains another stdClass object as one of its properties. The inner stdClass object has properties named "001" and "002" with corresponding values "John" and "Jenny".
To access and check the properties of the inner stdClass object, you can use the following code:
php
// Assuming $result contains the cURL response
if ($result instanceof stdClass && isset($result->id)) {
if ($result->id->{'001'} === 'John') {
echo 'John';
} elseif ($result->id->{'002'} === 'Jenny') {
echo 'Jenny';
}
}
In this code snippet, we are checking if the $result is an instance of stdClass and if it has the property "id". Then, we are accessing the properties "001" and "002" of the "id" object using curly braces and checking their values to print either "John" or "Jenny" based on the condition.
Rate this post
4 of 5 based on 6632 votesComments