Home>Article>Backend Development> How to convert PHP object to array?
#How to convert PHP object to array?
First use the function "json_encode()" to convert the object into a JSON string; then assign the return value of "json_encode" to a new variable; finally use "json_decode()" to convert the value of the new variable Just convert the JSON string into an array.
Sample code
$param = json_encode($param); $param = json_decode($param, true);
Other methods
function object_to_array($obj) { $arr = (array)$obj; foreach ($arr as $k => $v) { if (gettype($v) == 'resource') { return; } if (gettype($v) == 'object' || gettype($v) == 'array') { $arr[$k] = (array)object_to_array($v); } } return $arr; }
Recommended tutorial: "PHP"
The above is the detailed content of How to convert PHP object to array?. For more information, please follow other related articles on the PHP Chinese website!