Home  >  Article  >  Backend Development  >  How to convert object into array using php

How to convert object into array using php

PHPz
PHPzOriginal
2023-03-27 19:05:551359browse

PHP is an object-oriented programming language. Objects are one of the most basic data structures in PHP and one of the most commonly used data types. In PHP, it is usually a very common operation to convert objects into arrays, such as storing objects in a database, or serializing objects, etc. This article will introduce how to convert PHP objects into arrays.

1. Use the get_object_vars() function

PHP provides the get_object_vars() function, which can convert the object's attribute value into an associative array. This function receives an object as a parameter and returns an array whose keys are the names of the object's properties and whose values ​​are the values ​​of the properties. The following is an example:

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('Tom', 18, 'male');
$arr = get_object_vars($person);
print_r($arr);

The output result is:

Array
(
    [name] => Tom
    [age] => 18
    [gender] => male
)

2. Use the json_encode() function

In addition to using the get_object_vars() function , we can also use the json_encode() function to convert the object to a JSON-formatted string, and then use the json_decode() function to convert it to an array. Here is an example:

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('Tom', 18, 'male');
$jsonStr = json_encode($person);
$arr = json_decode($jsonStr, true);
print_r($arr);

The output is the same as the previous example.

3. Use the __toArray() method of the object

If you have mastered the basic knowledge of object-oriented programming, then you can define a _ in your own class _toArray() method, which is used to convert the object's attribute value into an array. The following is an example:

class Person {
    public $name;
    public $age;
    public $gender;
    public function __construct($name, $age, $gender) {
        $this->name = $name;
        $this->age = $age;
        $this->gender = $gender;
    }
    public function __toArray() {
        $arr = [];
        $arr['name'] = $this->name;
        $arr['age'] = $this->age;
        $arr['gender'] = $this->gender;
        return $arr;
    }
}
$person = new Person('Tom', 18, 'male');
$arr = $person->__toArray();
print_r($arr);

The output result is also the same as the previous two examples.

Summary

The above are the three methods of converting PHP objects into arrays. Different methods may be suitable for different application scenarios, and which method to use depends on your needs. No matter which method you use, mastering these methods is a very important skill in PHP development, which can facilitate us to operate objects and improve development efficiency.

The above is the detailed content of How to convert object into array using php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn