简体   繁体   中英

Converting a php object into associative array

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] => info@example.com
                            [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] => info@example.com
                                            [primary] => 1
                                        )

                                )

                            [nonEditableAliases] => Array
                                (
                                    [0] => info@example.com.test-google-a.com
                                )

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

                    [1] => Array
                        (
                            [kind] => admin#directory#user
                            [id] => 3895729453245
                            [etag] => "fsajdfd64hkj4534h5k3454"
                            [primaryEmail] => user@example.com
                            [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] => support@example.com
                                        )

                                    [1] => Array
                                        (
                                            [address] => help@example.com
                                        )

                                )

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


                )

        )

    [processed:protected] => Array
        (
        )

)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM