How to convert an object into an array in php

PHPz
Release: 2023-04-24 16:03:57
Original
462 people have browsed it

In PHP, objects and arrays are very commonly used data types. An object is a structure that encapsulates properties and methods, while an array is an ordered set of key-value pairs. In some scenarios, we may need to convert an object into an array object to facilitate data operations.

In PHP, a forced type conversion operator is used to convert an object into an array. The operator is "()" (bracket). At the same time, we can add the prefix "array" inside the bracket to specify the array key name:

$array = (array) $object;        //强制类型转换,使用默认键名
$array = (array)($object);       //同上
$array = (array) $object_arrray; //强制类型转换并指定键名数组
Copy after login

If the key name is not specified, the cast operator will use the default method to convert the object into an array. Specifically, each object attribute is added to the array as a key-value pair, and the key The name is the attribute name, and the key value is the attribute value. If the object contains other objects, these objects will be recursively converted into arrays. The following code:

class Person
{
    public $name = "David";
    public $age = 32;
    public $profession = "Software Engineer";
}

class Company
{
    public $name = "ABC Company";
    public $employees;
    
    public function __construct() {
        $this->employees = array(
            new Person(),
            new Person(),
            new Person()
        );
    }
}

$company = new Company();
$array = (array)($company);

print_r($array);
Copy after login

The output result is as follows:

Array
(
    [name] => ABC Company
    [employees] => Array
        (
            [0] => Person Object
                (
                    [name] => David
                    [age] => 32
                    [profession] => Software Engineer
                )

            [1] => Person Object
                (
                    [name] => David
                    [age] => 32
                    [profession] => Software Engineer
                )

            [2] => Person Object
                (
                    [name] => David
                    [age] => 32
                    [profession] => Software Engineer
                )

        )

)
Copy after login

As you can see, the object $company is forced to be converted into an array $array, Also contains all properties of the $company object.

In the above example, we can find that the cast only converts the object's properties into the key-value pairs of the array, and the object's methods will not be converted. If we need to convert the object's method into an array, we need to implement it through the magic methods __sleep() and __wakeup() in the class. Among them, the __sleep() method is used to save all properties of the object into an array, while the __wakeup() method is used to convert the saved array back to an object.

class Person
{
    public $name = "David";
    public $age = 32;
    public $profession = "Software Engineer";
    
    public function run() {
        echo "I am running...";
    }
    
    public function sleep() {
        echo "I am sleeping...";
    }
    
    public function __sleep() {
        return array(
            "name",
            "age",
            "profession"
        );
    }
    
    public function __wakeup() {
    }
}

$person = new Person();
$array = (array)($person);

print_r($array);
Copy after login

The output result is as follows:

Array
(
    [name] => David
    [age] => 32
    [profession] => Software Engineer
)
Copy after login

It can be found that the output result only contains the attributes of the $person object, and the methods are ignored.

To sum up, in PHP, we can convert an object into an array object by using the cast operator and the __sleep() and __wakeup() methods. , to achieve more flexible data manipulation.

The above is the detailed content of How to convert an object into an array in php. 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