1

I am trying to read the following php object result of an api call into an associative array. Has someone done this before or can otherwise help me?

I have tried object var dump, and array casting, but this just doesnt seems to work. I know I am doing something fundamentally wrong. Can someone help?

Many thanks.

Google_Service_Directory_Users Object
(
    [etag] => "sfgsfgdsfgsdfgjkdjgfd"
    [kind] => admin#directory#users
    [nextPageToken] => 
    [triggerEvent] => 
    [usersType:protected] => Google_Service_Directory_User
    [usersDataType:protected] => array
    [collection_key:protected] => items
    [modelData:protected] => Array
        (
            [users] => Array
                (
                    [0] => Array
                        (
                            [kind] => admin#directory#user
                            [id] => 7642341239423
                            [etag] => "jasdfjshwer43537345fsdfs"
                            [primaryEmail] => [email protected]
                            [name] => Array
                                (
                                    [givenName] => Info -
                                    [familyName] => Example
                                    [fullName] => Info - Example
                                )

                            [isAdmin] => 
                            [isDelegatedAdmin] => 
                            [lastLoginTime] => 2014-07-29T08:46:28.000Z
                            [creationTime] => 2014-07-29T08:31:56.000Z
                            [agreedToTerms] => 1
                            [suspended] => 
                            [changePasswordAtNextLogin] => 
                            [ipWhitelisted] => 
                            [emails] => Array
                                (
                                    [0] => Array
                                        (
                                            [address] => [email protected]
                                            [primary] => 1
                                        )

                                )

                            [nonEditableAliases] => Array
                                (
                                    [0] => [email protected]
                                )

                            [customerId] => fsdfdd4
                            [orgUnitPath] => /
                            [isMailboxSetup] => 1
                            [includeInGlobalAddressList] => 1
                        )

                    [1] => Array
                        (
                            [kind] => admin#directory#user
                            [id] => 3895729453245
                            [etag] => "fsajdfd64hkj4534h5k3454"
                            [primaryEmail] => [email protected]
                            [name] => Array
                                (
                                    [givenName] => User
                                    [familyName] => Name
                                    [fullName] => User Name
                                )

                            [isAdmin] => 1
                            [isDelegatedAdmin] => 
                            [lastLoginTime] => 2014-08-26T09:05:49.000Z
                            [creationTime] => 2012-09-16T08:55:26.000Z
                            [agreedToTerms] => 1
                            [suspended] => 
                            [changePasswordAtNextLogin] => 
                            [ipWhitelisted] => 
                            [emails] => Array
                                (
                                    [0] => Array
                                        (
                                            [address] => [email protected]
                                        )

                                    [1] => Array
                                        (
                                            [address] => [email protected]
                                        )

                                )

                            [customerId] => fsdafwr4
                            [orgUnitPath] => /
                            [isMailboxSetup] => 1
                            [includeInGlobalAddressList] => 1
                        )


                )

        )

    [processed:protected] => Array
        (
        )

)
4
  • 5
    The better question in my mind is why do you need to turn this object into an associative array? what gain would you have? Commented Aug 26, 2014 at 16:45
  • I want to extract the fields like family and given names, primary email etc. and use it in my app. Commented Aug 26, 2014 at 16:53
  • so what's stopping you from doing that? echo $obj['modelData']['users'][0]['name']['givenName'] will spit out "Info -"` attributes in a php object can be treated as if they were in an array. Commented Aug 26, 2014 at 16:56
  • Awesome! Thank you, that's precisely what I was looking for. Commented Aug 26, 2014 at 17:06

1 Answer 1

1

First solution:

$array = json_decode(json_encode($nested_object), true);

Second solution:

function object_to_array($data) {

    if (is_array($data) || is_object($data)):

        $result = array();

        foreach ($data as $key => $value)
            $result[$key] = object_to_array($value);

        return $result;

    endif;

    return $data;
}

both searched from the internetz

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

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.