How do I convert an object to an array?

2018-12-31 12:40发布

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

Outputs the following:

    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
    )...
)

Is this possible?

标签: php
11条回答
冷夜・残月
2楼-- · 2018-12-31 13:30

I had the same problem and I solved it with get_object_vars mentioned above.

Furthermore, I had to convert my object with json_decode and I had to iterate the array with the oldschool "for" loop (rather then for-each).

查看更多
还给你的自由
3楼-- · 2018-12-31 13:31
//My Function is worked. Hope help full for you :)
      $input = [
            '1' => (object) [1,2,3],
            '2' => (object) [4,5,6,
                (object) [6,7,8,
                [9, 10, 11,
                    (object) [12, 13, 14]]]
            ],
            '3' =>[15, 16, (object)[17, 18]]
        ];

        echo "<pre>";
        var_dump($input);
        var_dump(toAnArray($input));

      public function toAnArray(&$input) {

        if (is_object($input)) {
            $input = get_object_vars($input);
        }
        foreach ($input as &$item) {
            if (is_object($item) || is_array($item)) {
                if (is_object($item)) {
                    $item = get_object_vars($item);
                }
                self::toAnArray($item);
            }
        }
    }
查看更多
只靠听说
4楼-- · 2018-12-31 13:32

I ran into an issue with Andy Earnshaw's answer because I had factored this function out to a separate class within my application, "HelperFunctions", which meant the recursive call to objectToArray() failed.

I overcame this by specifying the class name within the array_map call like so:

public function objectToArray($object) {
    if (!is_object($object) && !is_array($object))
        return $object;
    return array_map(array("HelperFunctions", "objectToArray"), (array) $object);
}

I would have written this in the comments but I don't have enough reputation yet.

查看更多
荒废的爱情
5楼-- · 2018-12-31 13:33

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));
?>
查看更多
路过你的时光
6楼-- · 2018-12-31 13:42

Try this:-

 <?php
  print_r(json_decode(json_encode($response->response->docs),true));
 ?>
查看更多
登录 后发表回答