How do I convert an object to an array?

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

Outputs the following:

    Array 
(
    [0] => Object 
            (         


        
11条回答
  •  太阳男子
    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.

提交回复
热议问题