How to convert class into array and object array in php

PHPz
Release: 2023-04-20 15:11:41
Original
660 people have browsed it

PHP is a widely used server-side scripting language commonly used for web development. In PHP development, it is often necessary to convert an object into an array, or assign a value in an array to an object's properties. This article explains how to convert classes into arrays and object arrays.

1. Convert the class into an array

We first define a class Person, including name, gender, age and other information:

class Person {
    public $name;
    public $gender;
    public $age;
}
Copy after login

Now, we need to convert this class into an array for easy storage and transfer of data. We can use PHP's built-in function get_object_vars(), which will return all properties of the object. We can use this function to obtain the attribute array of the Person object, and then process the array:

$person = new Person();
$person->name = 'Tom';
$person->gender = 'male';
$person->age = 20;

$personArray = get_object_vars($person);
print_r($personArray);
Copy after login

The output result is as follows:

Array
(
    [name] => Tom
    [gender] => male
    [age] => 20
)
Copy after login

Through the get_object_vars() function, we convert the Person object into a Array $personArray, the key name corresponds to the object's attribute name, and the key value corresponds to the attribute value.

2. Convert an array into an object array

Next, we will introduce how to convert an array into an object array. Let's take the Person class as an example. Suppose we have an array containing multiple personnel information. Each personnel information is represented by an associative array:

$personList = [
    [
        'name' => 'Tom',
        'gender' => 'male',
        'age' => 20
    ],
    [
        'name' => 'Jerry',
        'gender' => 'male',
        'age' => 25
    ],
    [
        'name' => 'Lucy',
        'gender' => 'female',
        'age' => 18
    ]
];
Copy after login

We need to convert this array into an array of Person objects so that Carry out relevant operations. We can use the foreach statement to traverse this array, then convert each associative array into a Person object, and finally store these objects in a new array:

$personObjectList = [];

foreach ($personList as $person) {
    $personObject = new Person();
    $personObject->name = $person['name'];
    $personObject->gender = $person['gender'];
    $personObject->age = $person['age'];

    array_push($personObjectList, $personObject);
}

print_r($personObjectList);
Copy after login

The output results are as follows:

Array
(
    [0] => Person Object
        (
            [name] => Tom
            [gender] => male
            [age] => 20
        )

    [1] => Person Object
        (
            [name] => Jerry
            [gender] => male
            [age] => 25
        )

    [2] => Person Object
        (
            [name] => Lucy
            [gender] => female
            [age] => 18
        )

)
Copy after login

Passed To convert an associative array into a Person object, we convert the original array into an array of Person objects $personObjectList. The attribute name and attribute value in each object correspond to the key name and key value in the original array.

3. Conclusion

This article introduces how to convert classes into arrays and arrays into objects. These techniques are very commonly used in PHP development and can help us store and manipulate data more conveniently. I hope this article can help you in your development work.

The above is the detailed content of How to convert class into array and object 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!