Home  >  Article  >  Backend Development  >  How to convert objects to arrays and object arrays in php

How to convert objects to arrays and object arrays in php

PHPz
PHPzOriginal
2023-04-26 10:29:47571browse

With the rapid development of the Internet, PHP, as a programming language widely used in Internet technology development, has been widely used for its advantages of being easy to learn, strong scalability, and open source.

In PHP, objects and arrays are common data types. In actual development, it is often necessary to convert between them. This article will introduce how to convert objects into arrays and object arrays in PHP.

1. Convert objects into arrays

The difference between objects and arrays is that objects store properties and methods, while arrays store key-value pairs. Therefore, when converting an object to an array, the properties of the object need to be converted into key-value pairs of the array.

In PHP, you can use cast to convert an object into an array. The specific code is as follows:

$obj = new stdClass();
$obj->name = 'Tom';
$obj->age = 20;

$arr = (array)$obj;
print_r($arr);

In the above code, a stdClass object is first created, which contains two attributes, namely $name and $age. Then, use a cast to convert the object to an array.

Run the above code, the output result is as follows:

Array
(
    [name] => Tom
    [age] => 20
)

As can be seen from the output result, the $obj object is successfully converted into an array. In the array, the attribute name of the $obj object is converted to the key name in the array, and the attribute value is converted to the key value in the array.

2. Convert the object into an object array

An object array refers to an array containing multiple objects. In PHP, you can use a foreach loop to iterate through an array of objects, convert each object into an array and store it in a new array.

The specific code is as follows:

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

$person1 = new Person('Tom', 20);
$person2 = new Person('Jack', 25);
$person3 = new Person('Lucy', 18);

$persons = array($person1, $person2, $person3);

$arr = array();
foreach ($persons as $person) {
    $arr[] = (array)$person;
}

print_r($arr);

In the above code, a Person class is first defined, which contains two attributes, $name and $age, and a constructor to initialize the object. Subsequently, three Person objects are created and stored in an array $persons.

Next, create an empty array $arr, use foreach to traverse the $persons array, convert each object into an array and store it in $arr.

Run the above code, the output result is as follows:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 20
        )
    [1] => Array
        (
            [name] => Jack
            [age] => 25
        )
    [2] => Array
        (
            [name] => Lucy
            [age] => 18
        )
)

It can be seen from the output result that the three Person objects were successfully converted into an object array. In the array, each object is converted into an array containing the attribute key name and attribute key value.

Summary

Through the above introduction, we can see that the method of converting objects to arrays and object arrays in PHP is very simple. You only need to use forced type conversion and foreach loop. . In actual development, both methods have certain practical value and can help us better process data.

At the same time, it is also necessary to note that when converting an object into an array, the properties of the object need to be converted into key-value pairs of the array. The converted array may have duplicate key names leading to data loss. Therefore, the above methods need to be used with caution in actual development.

The above is the detailed content of How to convert objects to arrays and object arrays in 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