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

How to convert object into array in php?

青灯夜游
青灯夜游Original
2020-11-05 14:40:264269browse

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

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

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!

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