如何将对象转换为数组? [英] How do I convert an object to an array?

查看:86
本文介绍了如何将对象转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
   print_r($response->response->docs);
?>

输出以下内容:

    Array 
(
    [0] => Object 
            (
                [_fields:private] => Array 
                                    (
                                        [id]=>9093 
                                        [name]=>zahir
                                    ) 
            Object 
            ( 
                [_fields:private] => Array 
                                    (
                                        [id]=>9094 
                                        [name]=>hussain
                                    )..
            )
)

如何将该对象转换为数组?我想输出以下内容:

How can I convert this object to an array? I'd like to output the following:

Array
(
    [0]=>
    (
        [id]=>9093 
        [name]=>zahir
    ) 
    [1]=>
    (
        [id]=>9094 
        [name]=>hussain
    )...
)

这可能吗?

推荐答案

您应该查看

You should look at get_object_vars , as your properties are declared private you should call this inside the class and return its results.

请注意,对于像字符串这样的原始数据类型,它会很好用,但我不知道它在嵌套对象中的表现.

Be careful, for primitive data types like strings it will work great, but I don't know how it behaves with nested objects.

在您的情况下,您必须执行类似操作;

in your case you have to do something like;

<?php
   print_r(get_object_vars($response->response->docs));
?>

这篇关于如何将对象转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆