Home >Backend Development >PHP Problem >How to convert object into array in php?
Conversion method: First use json_encode() to convert the object into json format data; then use json_decode() to convert the json format data into an array; the syntax format is "json_decode(json_encode(object),true );".
Recommended: "PHP Video Tutorial"
php converts objects into arrays Method
1. Use the system’s built-in function to convert
$arr=json_decode(json_encode($object),true); var_dump($arr);
json_encode() function can convert the data format of objects and arrays into json Format data
json_decode() function can convert json format data into objects and arrays. To convert into an array, add true
2. Encapsulate custom functions and pass in objects
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; }
For more programming-related knowledge, please visit: Programming Learning Website! !
The above is the detailed content of How to convert object into array in php?. For more information, please follow other related articles on the PHP Chinese website!