Home > Article > Backend Development > How to convert php object into array
How to convert PHP objects into arrays: 1. Use the encapsulated custom function and pass in the object. The code is [$array[$key] =$this->object_array($value)]; 2. Use the system function to convert, the code is [$arr=json_decode()].
Method to convert php object into array:
1. Encapsulate custom function and pass in object
public function object_array($object) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$key] =$this->object_array($value); } } return $array; }
2. Use system function conversion
$arr=json_decode(json_encode($object),true); var_dump($arr);
Related video recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to convert php object into array. For more information, please follow other related articles on the PHP Chinese website!