0

If i have a php object:

  stdClass Object
 (
[userAttributes] => stdClass Object
    (
        [dog] => stdClass Object
            (
                [type] => Canine
                [required] => *
                [options] => 
                [size] => 
            )

        [cat] => stdClass Object
            (
                [type] => Feline
                [required] => *
                [options] => 
                [size] => 
            )
       )
 )

without doing a foreach loop, is it possible to print the value of the children of userAttributes. I want to print "dog" and "cat".

if this was an array:

  $userAttributes['dog'] = array('type'=>'Canine'.....);

i could do key($userAttributes) and get the word dog. is there a php function that does the same thing as key on objects?

2 Answers 2

1

You can use get_object_vars:

$properties = get_object_vars( new yourFunction() );

print_r( $properties );

Also you can try (array) $obj cast to array as:

var_dump((array) $obj);

Sign up to request clarification or add additional context in comments.

2 Comments

yea, that prints everything. I want to print just the word "dog" or "cat".
Try print_r( $properties['dog']);
0

Try below code:

print_r($yourObject->userAttributes->dog);

EDIT:

print_r(array_keys($yourObject->userAttributes)); // will print dog, cat

https://www.php.net/array_keys

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.