Home >Backend Development >PHP Problem >How to convert object to array in php
In PHP, we usually have some scenarios where we need to convert objects into arrays. For example, when storing and transferring data, we need to convert objects into arrays. PHP provides some convenient methods to accomplish this operation, the most commonly used method is get_object_vars()
.
get_object_vars()
The method can obtain all member variables in the object and return them in the form of an associative array. Here is an example:
class Person { public $name = ""; public $age = ""; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $person = new Person("John Doe", 30); $array = get_object_vars($person); print_r($array);
Output:
Array ( [name] => John Doe [age] => 30 )
In the above code, we create a Person
class and instantiate it as $person
object. Then we called the get_object_vars($person)
method and assigned its return value to the $array
variable. Finally, we printed the $array
array using the print_r()
function.
In addition to the get_object_vars()
method, PHP also provides some other methods for converting objects into arrays. For example:
json_decode(json_encode($obj), true)
: Convert the object to a JSON string, and then convert the JSON string to an array. This method is very flexible and can convert multidimensional objects into multidimensional arrays. iterator_to_array($obj)
: Convert an object that implements the Iterator
interface into an array. objectToArray()
: This is a custom method that can recursively convert all nested objects into arrays. The following is an example of using the json_decode()
method to convert an object into an array:
class Person { public $name = ""; public $age = ""; public $address = null; public function __construct($name, $age, $address) { $this->name = $name; $this->age = $age; $this->address = $address; } } class Address { public $city = ""; public $country = ""; public function __construct($city, $country) { $this->city = $city; $this->country = $country; } } $address = new Address("Los Angeles", "USA"); $person = new Person("John Doe", 30, $address); $array = json_decode(json_encode($person), true); print_r($array);
Output result:
Array ( [name] => John Doe [age] => 30 [address] => Array ( [city] => Los Angeles [country] => USA ) )
above In the code, we created a Person
class and a Address
class to represent people and addresses respectively. Then we created a $address
object and a $person
object, and assigned the address object to the $address
of the Person
object. Member variables. Finally, we use the json_decode()
method to convert the $person
object into a JSON string, then convert the JSON string into an array, and assign it to $array
Array. Finally, we use the print_r()
function to print the $array
array.
In general, converting objects into arrays is a very practical skill in PHP development. We can use this skill to process data conveniently when we need to perform operations such as data storage and transfer. The several methods introduced here are very simple and easy to understand, and can be selected and used according to the actual situation.
The above is the detailed content of How to convert object to array in php. For more information, please follow other related articles on the PHP Chinese website!