How to convert PHP object to array?

Guanhui
Release: 2023-03-02 18:04:01
Original
2639 people have browsed it


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);
Copy after login

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;

}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!