Home > Backend Development > PHP Problem > How to convert object into array in php?

How to convert object into array in php?

青灯夜游
Release: 2023-03-07 11:24:01
Original
4247 people have browsed it

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 );".

How to convert object into array in php?

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

json_encode() function can convert the data format of objects and arrays into json Format data

How to convert object into array in php?

json_decode() function can convert json format data into objects and arrays. To convert into an array, add true

How to convert object into array in php?

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

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!

Related labels:
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template