Home >Backend Development >PHP Problem >How to convert php object into array
In PHP, objects and arrays are very important data types. Objects are usually used to represent instances of a class, while arrays are used to store multiple values. Sometimes, we need to convert a PHP object into an array to make it easier to operate on it. This article will explain how to convert PHP objects into arrays.
1. Use forced type conversion
In PHP, you can use the forced type conversion operator to convert an object into an array. This symbol is two parentheses placed in front of the object, for example:
$array = (array) $object;
Doing this will convert all the properties and values of the object into an array. The properties of the object will become the keys of the array, and the values of the corresponding properties will become the values of the array.
However, there are some limitations to using this method to convert objects. First, private and protected properties cannot be converted, only public properties can. Secondly, if the object nests other objects, you need to define the string representation in the class's __toString()
method, otherwise it will not be converted correctly.
2. Using object iterator
PHP’s object iterator is a special interface that allows an object to iterate its properties and values in a specific way. By implementing the iterator interface, we can convert an object into an array instead of using a cast.
The following is a simple example that demonstrates how to use the iterator interface to convert an object to an array:
class User implements Iterator { private $data = ['name' => 'John', 'email' => 'john@example.com']; private $position = 0; public function rewind() { $this->position = 0; } public function current() { $keys = array_keys($this->data); $key = $keys[$this->position]; return [ 'key' => $key, 'value' => $this->data[$key] ]; } public function key() { $keys = array_keys($this->data); return $keys[$this->position]; } public function next() { ++$this->position; } public function valid() { $keys = array_keys($this->data); return isset($keys[$this->position]); } public function toArray() { $data = array(); foreach ($this as $key => $value) { $data[$value['key']] = $value['value']; } return $data; } } $user = new User(); print_r($user->toArray());
In this example, we implement the Iterator
interface, and Return the key-value pair of the object's properties in the current()
method. Then, in the toArray()
method, we use a foreach
loop to create a new array.
We can convert an object into an array by implementing the iterator interface and using a foreach
loop in the toArray()
method.
3. Use(array) json_decode()
If a class is complex and it contains multiple nested objects, then implementing the iterator interface may Very troublesome. In this case, we can use a combination of json_decode()
and (array)
casts to convert the object to an array.
The sample code is as follows:
class User { private $name = "John"; private $email = "john@example.com"; private $address; public function __construct() { $this->address = new Address("100 Main St", "New York"); } } class Address { private $street; private $city; public function __construct($street, $city) { $this->street = $street; $this->city = $city; } } $user = new User(); $array = (array) json_decode(json_encode($user), true); print_r($array);
In this example, we define a User
class and an Address
class. The User
class contains an Address
object. We then use json_encode()
to convert the $user
object to a JSON string and the (array)
cast to convert it to an array. Finally, we use print_r()
to output the array.
Since the JSON format is almost the same as the format of a PHP array, when we use the (array) json_decode()
caster, the JSON string is converted into a PHP array.
4. Use get_object_vars()
If a class does not inherit a parent class and it does not have nested objects, you can use PHP’s built-in get_object_vars()
function Convert object to array. This function returns all properties and values of the object, excluding non-public properties and methods.
The sample code is as follows:
class User { public $name = "John"; public $email = "john@example.com"; } $user = new User(); $array = get_object_vars($user); print_r($array);
In this example, we define a User
class, which contains two public properties $name
and $email
. We then created a $user
object and converted it to an array using the get_object_vars()
function. Finally, we use the print_r()
function to output the array.
Summary
In PHP, we can use various methods to convert objects into arrays. If a class is simple and has no nested objects, it can be converted to an array using the get_object_vars()
function. If a class is more complex, you can implement the iterator interface and use a foreach
loop in the toArray()
method to convert to an array. If a class contains nested objects, it can be converted to a JSON string and converted to an array using the (array) json_decode()
cast.
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!