How do I convert an object to an array?

后端 未结 11 1271
别那么骄傲
别那么骄傲 2020-11-22 10:36
response->docs);
?>

Outputs the following:

    Array 
(
    [0] => Object 
            (         


        
相关标签:
11条回答
  • 2020-11-22 10:52

    Try this:-

     <?php
      print_r(json_decode(json_encode($response->response->docs),true));
     ?>
    
    0 讨论(0)
  • 2020-11-22 10:53

    Single-dimensional arrays

    For converting single-dimension arrays, you can cast using (array) or there's get_object_vars, which Benoit mentioned in his answer.

    // Cast to an array
    $array = (array) $object;
    
    // get_object_vars
    $array = get_object_vars($object);
    

    They work slightly different from each other. For example, get_object_vars will return an array with only publicly accessible properties unless it is called from within the scope of the object you're passing (ie in a member function of the object). (array), on the other hand, will cast to an array with all public, private and protected members intact on the array, though all public now, of course.

    Multi-dimensional arrays

    A somewhat dirty method is to use PHP >= 5.2's native JSON functions to encode to JSON and then decode back to an array. This will not include private and protected members, however, and is not suitable for objects that contain data that cannot be JSON encoded (such as binary data).

    // The second parameter of json_decode forces parsing into an associative array
    $array = json_decode(json_encode($object), true);
    

    Alternatively, the following function will convert from an object to an array including private and protected members, taken from here and modified to use casting:

    function objectToArray ($object) {
        if(!is_object($object) && !is_array($object))
            return $object;
    
        return array_map('objectToArray', (array) $object);
    }
    
    0 讨论(0)
  • 2020-11-22 10:55
    $array = json_decode(json_encode($object), true);
    

    I tried several ways to do a foreach with an object and THIS really is the most easy and cool workaround I have seen. Just one line :)

    0 讨论(0)
  • 2020-11-22 11:00

    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.

    0 讨论(0)
  • 2020-11-22 11:01

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

    0 讨论(0)
  • 2020-11-22 11:01

    You can also use array_values() method of php

    0 讨论(0)
提交回复
热议问题