简体   繁体   English

将php对象转换为关联数组

[英]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. 我正在尝试将以下api对象调用的php对象结果读入关联数组。 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. 我尝试了对象var dump和数组转换,但是这似乎不起作用。 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 都从网际网路搜寻

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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