Home  >  Article  >  Backend Development  >  How to convert php object into array

How to convert php object into array

coldplay.xixi
coldplay.xixiOriginal
2020-08-05 11:46:543793browse

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;
}

2. Use system function conversion

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

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!

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