小编典典

如何将对象转换为数组?

php

response->docs); ?>

输出以下内容:

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

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

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

这可能吗?


阅读 593

收藏
2020-05-26

共1个答案

小编典典

您应该查看get_object_vars,因为您的属性被声明为私有,所以您应该在类内部调用此方法并返回其结果。

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

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

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