Home>Article>Backend Development> How to convert PHP object to array?

How to convert PHP object to array?

Guanhui
Guanhui Original
2020-07-18 15:00:02 2718browse


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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn