How to convert array object to object array in php

PHPz
Release: 2023-04-24 16:14:02
Original
405 people have browsed it

During the PHP development process, sometimes it is necessary to convert an object represented by an array into an object array so that it can be manipulated using object-oriented methods. This article explains how to implement this feature.

First, we assume that there is an object represented by an array, similar to the following:

$user = [
    'id' => 1,
    'name' => 'Jack',
    'age' => 28,
];
Copy after login

This object represents a user and contains the user's ID, name and age attributes. Now we need to convert it into a User object so that we can manipulate it using object-oriented methods.

We can define a User class that contains ID, name and age attributes, and define the corresponding constructor:

class User {
    public $id;
    public $name;
    public $age;

    public function __construct($id, $name, $age) {
        $this->id = $id;
        $this->name = $name;
        $this->age = $age;
    }
}
Copy after login

Then, we can write a function to convert objects represented by arrays Into a User object:

function arrayToObject($array) {
    $objectArray = [];

    foreach ($array as $item) {
        $user = new User($item['id'], $item['name'], $item['age']);
        array_push($objectArray, $user);
    }

    return $objectArray;
}
Copy after login

This function accepts an object represented by an array as a parameter, and uses a foreach loop to traverse each item in the array. For each item, it creates a new User object and adds it to an array of objects. Finally, it returns an array of objects.

Now, we can use this function to convert an object represented by an array into an object array:

$users = [
    [
        'id' => 1,
        'name' => 'Jack',
        'age' => 28,
    ],
    [
        'id' => 2,
        'name' => 'John',
        'age' => 32,
    ],
    [
        'id' => 3,
        'name' => 'Lily',
        'age' => 25,
    ],
];

$objectArray = arrayToObject($users);
Copy after login

This code converts each item in the users array into a User object, And store the result in objectArray array. Now, we can use object-oriented methods to operate this object array:

foreach($objectArray as $user) {
    echo $user->name . ' is ' . $user->age . ' years old.';
}
Copy after login

This code will traverse each User object in the objectArray array and output the name and age of each user.

To summarize, converting an object represented by an array into an object array allows us to use object-oriented methods to operate it, making the code more concise, readable, and maintainable. We can easily convert an object represented by an array into an array of objects by defining a class and corresponding constructor, and writing a function that converts the array into an object.

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