简体   繁体   English

如何将字符串作为stdClass对象转换为数组[PHP]

[英]How to convert string as stdClass object to array [PHP]

I'm coding a login system using API. 我正在使用API​​编写登录系统。 i'm stuck with the result that i got using the API. 我对使用API​​的结果感到困惑。 I got the result as normal string like this: 我得到的结果是这样的普通字符串:

stdClass Object
(
    [success] => 1
    [message] => Array
        (
            [code] => 000
            [txt] => Operation successful
        )

    [data] => Array
        (
            [result] => OK
            [accountID] => 000000000
            [group] => demoCAC
            [currency] => USD
            [enable] => 1
            [read_only] => 0
            [name] => Bogus Demo
            [country] => ****
            [city] => 
            [address] => 
            [phone] => **
            [email] => ***@gmail.com
            [status] => test
            [regdate] => 07/02/2017 09:57
            [lastdate] => 07/02/2017 09:57
            [balance] => 0.00
            [credit] => 0.00
            [equity] => 10000.00
            [isDemo] => 1
            [tp_version] => **
            [last_name] => Demo
            [first_name] => Bogus
            [domain] => ***.com
            [compliance] => 
            [latestTrades] => Array
                (
                    [results] => Array
                        (
                        )

                    [totalResults] => 0
                )

            [owner] => 
        )
    )

So how can i convert it to an array or useable variable ? 那么如何将其转换为数组或可用变量? * This information printed by ( echo ), and i can't access $result -> data ->... is there a function that convert this string to array or json or something readable so i can reach to specific value? *该信息由(echo)打印,并且我无法访问$ result-> data-> ...是否存在将此字符串转换为数组或json或可读性的函数,所以我可以达到特定的值? when i try : print_r(get_object_vars($result)); 当我尝试时:print_r(get_object_vars($ result)); i get this error: 我收到此错误:

Warning: get_object_vars() expects parameter 1 to be object, string given in /home/**/public_html/crm/profile.php on line 20 警告:get_object_vars()期望参数1为对象,在第20行的/home/**/public_html/crm/profile.php中给出的字符串

Unless Im missing something, your problem is a simple one. 除非我缺少任何东西,否则您的问题很简单。

looking at the guts of your var_dump, you should be able to access the index in your question like: 查看您的var_dump胆量,您应该可以访问问题中的索引,例如:

$result->data['email'];  // where 'email' is the key of the data.

or 要么

$result->data['latestTrades']['results']; // nested example.

all the properties of a standard class are public, and so can be accessed directly. 标准类的所有属性都是公共的,因此可以直接访问。 And in your example, all of them contain an array, or nested array so can be accessed by their key. 在您的示例中,它们全部包含一个数组或嵌套数组,因此可以通过其键进行访问。

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

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