Home > Article > Backend Development > How to convert object array into array in php
PHP is a very popular programming language that supports object-oriented programming (OOP) and array operations. In PHP, we sometimes need to convert an array of objects into a normal array. This article will introduce how to convert an array of objects into an array in PHP.
There is a very convenient function in PHP that can convert a JSON format string into a PHP array: json_decode.
Object arrays can be encoded into JSON format strings. Therefore, we can first encode the object array into a JSON string and then use the json_decode function to convert it into a PHP array. Here is an example:
<?php class Person { public $name; public $age; public $gender; } // 创建对象数组 $persons = []; $person1 = new Person(); $person1->name = "张三"; $person1->age = 20; $person1->gender = "男"; $persons[] = $person1; $person2 = new Person(); $person2->name = "李四"; $person2->age = 22; $person2->gender = "男"; $persons[] = $person2; // 将对象数组编码成JSON字符串 $json_str = json_encode($persons); // 将JSON字符串解码成PHP数组 $person_arr = json_decode($json_str, true); print_r($person_arr); ?>
This will output the following results:
Array ( [0] => Array ( [name] => 张三 [age] => 20 [gender] => 男 ) [1] => Array ( [name] => 李四 [age] => 22 [gender] => 男 ) )
Another way to convert an array of objects into an array The method is to implement the Serializable interface. This method requires a custom method in the class to convert the object array of this class into a serializable string. Here is an example:
<?php class Person implements Serializable { public $name; public $age; public $gender; // 实现Serializable接口的方法 public function serialize() { return serialize([$this->name, $this->age, $this->gender]); } // 实现Serializable接口的方法 public function unserialize($serialized) { [$this->name, $this->age, $this->gender] = unserialize($serialized); } } // 创建对象数组 $persons = []; $person1 = new Person(); $person1->name = "张三"; $person1->age = 20; $person1->gender = "男"; $persons[] = $person1; $person2 = new Person(); $person2->name = "李四"; $person2->age = 22; $person2->gender = "男"; $persons[] = $person2; // 将对象数组编码成可序列化的字符串 $serialized_str = serialize($persons); // 将序列化的字符串转换为PHP数组 $person_arr = unserialize($serialized_str); print_r($person_arr); ?>
This will output the following results:
Array ( [0] => Person Object ( [name] => 张三 [age] => 20 [gender] => 男 ) [1] => Person Object ( [name] => 李四 [age] => 22 [gender] => 男 ) )
Summary
This article introduces two methods of converting an array of objects into an array: using the json_decode function and Implement the Serializable interface. Developers can choose different implementation methods according to their own needs. Remember that when using the second method, the object class needs to implement the Serializable interface.
The above is the detailed content of How to convert object array into array in php. For more information, please follow other related articles on the PHP Chinese website!