Home > Article > Backend Development > Convert object to array in php
In PHP, an object is a complex data type that can contain multiple properties and methods. Sometimes we need to convert objects into arrays for operations, such as storing objects in a database or converting objects into JSON format. This article will explain how to convert objects to arrays in PHP.
Method 1: Use forced type conversion
You can use forced type conversion in PHP to convert an object into an array. The specific method is to use the properties of the object as the keys in the array, the value of the property as the value of the array, and then return the array.
The following is a sample code that implements the function of converting a Person object into an array:
class Person { public $name; public $age; public $gender; public function __construct($name, $age, $gender) { $this->name = $name; $this->age = $age; $this->gender = $gender; } } $person = new Person('John', 28, 'male'); // 使用强制类型转换将对象转换为数组 $array = (array) $person; print_r($array); // 输出结果为:Array ( [name] => John [age] => 28 [gender] => male )
As you can see, in the above example, we used forced type conversion to convert Person Object converted to array. Casting is very convenient, but it has a disadvantage, that is, it can only convert the public properties of the object into an array, but cannot convert private or protected properties into an array.
Method 2: Use the get_object_vars function
In addition to forced type conversion, PHP also provides another method to convert an object into an array. This method is to use the get_object_vars function. This function accepts an object as a parameter and converts all properties of the object into an array and returns it.
The following is a sample code that implements the function of converting a Person object into an array:
class Person { public $name; public $age; public $gender; public function __construct($name, $age, $gender) { $this->name = $name; $this->age = $age; $this->gender = $gender; } } $person = new Person('John', 28, 'male'); // 使用get_object_vars函数将对象转换为数组 $array = get_object_vars($person); print_r($array); // 输出结果为:Array ( [name] => John [age] => 28 [gender] => male )
Unlike forced type conversion, the get_object_vars function can convert all attributes of the object into an array, Includes private properties and protected properties.
Method 3: Use json_decode and json_encode functions
In addition to the above two methods, we can also convert the object to JSON format first, and then convert the JSON format into an array. This method requires the use of json_decode and json_encode functions.
The following is a sample code that implements the function of converting a Person object into an array:
class Person { public $name; public $age; public $gender; public function __construct($name, $age, $gender) { $this->name = $name; $this->age = $age; $this->gender = $gender; } } $person = new Person('John', 28, 'male'); // 将对象转换为JSON格式 $json = json_encode($person); // 将JSON格式转换为数组 $array = json_decode($json, true); print_r($array); // 输出结果为:Array ( [name] => John [age] => 28 [gender] => male )
By using the json_decode and json_encode functions, we can simply convert the object into an array.
Summary:
In PHP, there are many ways to convert objects into arrays. We can use methods such as cast, get_object_vars function, json_decode and json_encode functions. Different methods have different applicable scenarios, and we can choose the appropriate method according to actual needs. When using forced type conversion, you need to note that only public attributes can be converted into arrays; when using the get_object_vars function, you can convert all attributes into arrays; when using the json_decode and json_encode functions, you need to pay attention to the JSON format conversion.
The above is the detailed content of Convert object to array in php. For more information, please follow other related articles on the PHP Chinese website!