Home > Article > Backend Development > How to convert php object to string array
PHP is a very powerful programming language mainly used for server-side development, but also supports object-oriented programming definitions. When using PHP for object operations, sometimes you need to convert the object into a string or array to facilitate operation. This article will introduce how to convert PHP objects to strings and arrays, and give detailed code implementation and examples.
1. Convert PHP objects to strings
To convert PHP objects to strings, the magic method __toString() is usually used. This method is automatically called to convert the object into string form. The following is an example:
class Person { private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function __toString() { return "Name: " . $this->name . ", Age: " . $this->age; } } $person = new Person('John', 25); echo $person; // 输出 "Name: John, Age: 25"
2. Convert PHP objects into arrays
To convert PHP objects into arrays, you can use the object conversion tool json_decode() to convert the JSON string into an object, and then Then use the type conversion function to convert the object into an array. The following is an example:
class Book { public $title; public $author; public function __construct($title, $author) { $this->title = $title; $this->author = $author; } } $book = new Book('PHP Programming', 'John Smith'); // 将对象转换为 JSON 字符串 $json_string = json_encode($book); // 将 JSON 字符串转换为数组 $book_array = (array) json_decode($json_string); print_r($book_array); // 输出 Array ( [title] => PHP Programming [author] => John Smith )
The above is the simplest example of converting an object into an array. In actual application, different conversion operations need to be performed according to the specific situation.
3. Convert PHP objects into associative arrays
If you need to convert PHP objects into associative arrays, you need to use the object conversion tool get_object_vars() to convert the objects into arrays. Here is an example:
class Car { public $brand; public $model; public function __construct($brand, $model) { $this->brand = $brand; $this->model = $model; } } $car = new Car('Toyota', 'Rav4'); // 将对象转换为关联数组 $car_array = get_object_vars($car); print_r($car_array); // 输出 Array ( [brand] => Toyota [model] => Rav4 )
4. Summary
This article introduces how to convert PHP objects into strings and arrays. For converting an object into a string, you can use the magic method __toString(); for converting an object into an array, you can use the object conversion tool json_decode() to convert the object into a JSON string, or use the object conversion tool get_object_vars() to convert the object into an array. Object is converted to an associative array. Either way, you need to choose from them based on your specific needs.
The above is the detailed content of How to convert php object to string array. For more information, please follow other related articles on the PHP Chinese website!