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

How to convert php object into array

coldplay.xixi
Release: 2023-03-03 20:06:01
Original
3827 people have browsed it

How to convert PHP objects into arrays: 1. Use the encapsulated custom function and pass in the object. The code is [$array[$key] =$this->object_array($value)]; 2. Use the system function to convert, the code is [$arr=json_decode()].

How to convert php object into array

Method to convert php object into array:

1. Encapsulate custom function and pass in object

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

2. Use system function conversion

$arr=json_decode(json_encode($object),true);
 var_dump($arr);
Copy after login

Related video recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to convert php object into 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template