php将Object转换为清晰易读的数组

时间:2017-12-15 03:06:38

标签: php arrays object dingo-api

我有这个变量:$token = $resource->getToken();

这代表一个Dingo \ OAuth2 \ Entity \ Token Object:

Dingo\OAuth2\Entity\Token Object
(
    [attributes:protected] => Array
        (
            [token] => 4w8kCzaxUeqPC4bt1vIqcyea7cnOwkXMRljnrTLZ
            [type] => access
            [client_id] => gozfly-support-wvjausbh
            [user_id] => 2
            [expires] => 1513305079
            [scopes] => Array
                (
                    [accounts.profile.basic] => Dingo\OAuth2\Entity\Scope Object
                        (
                            [attributes:protected] => Array
                                (
                                    [scope] => accounts.profile.basic
                                    [name] => View profile basic information
                                    [description] => GOZFLY Accounts: View basic profile information
                                )

                        )

                    [accounts.profile.emailaddress] => Dingo\OAuth2\Entity\Scope Object
                        (
                            [attributes:protected] => Array
                                (
                                    [scope] => accounts.profile.emailaddress
                                    [name] => View profile email address information
                                    [description] => GOZFLY Accounts: View email address profile information
                                )

                        )

                )

        )

)

我需要将此对象转换为php数组,如此输出:

{
    "attributes": {
        "token": "4w8kCzaxUeqPC4bt1vIqcyea7cnOwkXMRljnrTLZ",
        "type": "access",
        "client_id": "gozfly-support-wvjausbh",
        "user_id": "2",
        "expires": 1513301754,
        "scopes": {
            "accounts.profile.basic": {
                "scope": "accounts.profile.basic",
                "name": "View profile basic information",
                "description": "GOZFLY Accounts: View basic profile information"
            },
            "accounts.profile.emailaddress": {
                "scope": "accounts.profile.emailaddress",
                "name": "View profile email address information",
                "description": "GOZFLY Accounts: View email address profile information"
            }
        }
    }
}

我尝试使用:(array)$ token但它没有正确进行转换,感谢任何帮助。

1 个答案:

答案 0 :(得分:1)