Home >Backend Development >PHP Problem >How to read object array in php
Object array refers to an array composed of multiple objects. Unlike general numeric arrays, object arrays are arrays composed of object references. In PHP, there are several ways to read an array of objects.
1. Use foreach loop to traverse object array
Using foreach loop to traverse object array is one of the most common methods. The sample code is as follows:
class User { public $id; public $name; public $email; } $user1 = new User(); $user1->id = 1; $user1->name = "John"; $user1->email = "john@example.com"; $user2 = new User(); $user2->id = 2; $user2->name = "Smith"; $user2->email = "smith@example.com"; $users = array($user1, $user2); foreach ($users as $user) { echo $user->id . " | " . $user->name . " | " . $user->email . "<br>"; }
In the above example, we created the User class and instantiated two objects $user1 and $user2, and then added these two objects to an object array $users. Next, use a foreach loop to loop through $users, store each object into the variable $user, and output the object's attribute value.
2. Use for loop to traverse object array
In addition to using foreach loop, you can also use for loop to traverse object array. The sample code is as follows:
class User { public $id; public $name; public $email; } $user1 = new User(); $user1->id = 1; $user1->name = "John"; $user1->email = "john@example.com"; $user2 = new User(); $user2->id = 2; $user2->name = "Smith"; $user2->email = "smith@example.com"; $users = array($user1, $user2); for ($i = 0; $i < count($users); $i++) { echo $users[$i]->id . " | " . $users[$i]->name . " | " . $users[$i]->email . "<br>"; }
In the above example, we created the User class and instantiated two objects $user1 and $user2, and then added these two objects to an object array $users. Next, use a for loop to traverse $users and output the attribute values of the object.
3. Use the array_map function to convert an object array into an array
In addition to using a loop to traverse the object array, you can also use the array_map function to convert an object array into a normal array. The sample code is as follows:
class User { public $id; public $name; public $email; } $user1 = new User(); $user1->id = 1; $user1->name = "John"; $user1->email = "john@example.com"; $user2 = new User(); $user2->id = 2; $user2->name = "Smith"; $user2->email = "smith@example.com"; $users = array($user1, $user2); $array_users = array_map(function($user) { return (array)$user; }, $users); foreach ($array_users as $user) { echo $user['id'] . " | " . $user['name'] . " | " . $user['email'] . "<br>"; }
In the above example, we created the User class and instantiated two objects $user1 and $user2, and then added these two objects to an object array $users. Convert each object to an array using the array_map function and assign it to a new array $array_users. Finally, use a foreach loop to loop through $array_users and output the values of the array elements.
Summary:
The above are several common methods for reading object arrays in PHP. Using a loop to iterate over an array of objects is one of the most common methods, but in some cases you may use the array_map function to convert an array of objects into a normal array. Different methods are suitable for different scenarios.
The above is the detailed content of How to read object array in php. For more information, please follow other related articles on the PHP Chinese website!