How to turn a php object into an array

PHPz
Release: 2023-05-24 19:52:35
Original
641 people have browsed it

In PHP, objects can be easily manipulated, but sometimes it is necessary to convert the object into an array or process the object's properties and methods in the form of an array. This article will detail how to convert PHP objects to arrays.

Generally speaking, there are two ways to convert an object into an array: one is to use the built-in method of the object to convert, and the other is to convert through a custom method.

Method 1: Use the built-in method of the object

PHP provides two built-in methods to convert the object into an array: get_object_vars()andjson_decode() . get_object_vars()The function returns an associative array containing all visible attributes. json_decode()The function can convert a JSON-formatted string into an array or object.

Sample code:

class Person {
    public $name;
    public $age;

    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
    }
}

$person = new Person('John', 25);

// 使用get_object_vars()方法
$person_array1 = get_object_vars($person);
print_r($person_array1);

// 使用json_decode()方法
$person_json = json_encode($person);    // 将对象转换为JSON字符串
$person_array2 = json_decode($person_json, true);
print_r($person_array2);
Copy after login

Output result:

Array
(
    [name] => John
    [age] => 25
)
Array
(
    [name] => John
    [age] => 25
)
Copy after login

As you can see, both methods can easily convert objects into arrays.

Method 2: Convert through custom methods

In addition to using built-in methods, we can also convert objects into arrays through custom methods. This approach is typically used when multiple properties need to be converted or additional processing of properties is required.

Sample code:

class Person {
    public $name;
    public $age;

    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function toArray() {
        return [
            'name' => strtoupper($this->name),
            'age' => $this->age,
        ];
    }
}

$person = new Person('John', 25);

// 使用自定义方法
$person_array = $person->toArray();
print_r($person_array);
Copy after login

Output result:

Array
(
    [name] => JOHN
    [age] => 25
)
Copy after login

As you can see, the object can be easily converted into an array through custom methods, and additional processing of attributes can be performed .

Summary

This article introduces two methods of converting PHP objects into arrays. For simple objects, just use built-in methods; for situations that require additional processing, you can use custom methods. No matter which method is used, it is very convenient and practical, and the object can be easily converted into an array for further operations.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!