将php对象转换为关联数组

时间:2014-08-26 16:42:38

标签: php

我正在尝试将api调用的以下php对象结果读入关联数组。有人曾经这样做过,或者可以帮助我吗?

我已经尝试过对象var转储和数组转换,但这似乎不起作用。我知道我在做一些根本错误的事情。有人可以帮忙吗?

非常感谢。

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
        (
        )

)

1 个答案:

答案 0 :(得分:1)

第一个解决方案:

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

第二个解决方案:

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;
}

都是从internetz搜索的

相关问题