In PHP development, arrays are one of the most common data types. In some cases, we may need to convert an ordinary array into an array object. This article will introduce how to achieve this operation.
- Creating Array Objects
The most common way to create array objects in PHP is to use the standard class stdClass. We can create an array object by instantiating stdClass.
$obj = new stdClass();
- Convert an array into an array object
Next we will use the PHP function array_map(), which applies a callback function to a given array and returns A new array whose elements are the return values of each function call. This function is our key to achieving this.
The sample code is as follows:
$arr = array( array('name' => '张三', 'age' => 23), array('name' => '李四', 'age' => 29), array('name' => '王五', 'age' => 26) ); $objArr = array_map(function($item){ $obj = new stdClass(); foreach ($item as $key => $val) { $obj->$key = $val; } return $obj; }, $arr);
In the above code, we define an array that contains three sub-arrays, each sub-array contains a name and age. We use the array_map() function to convert each subarray in the original array into a new stdClass object and place the object into the new array. In the callback function, we instantiate a stdClass object, assign the key-value pair of each subarray to this new instance, and then return the instance. After calling the array_map() function, the $objArr variable obtained is an array of array objects, and each element is a stdClass object.
- Using Array Objects
Once we successfully convert a normal array into an array of array objects, we can also use the array object just like a normal array. For example, we can iterate over an array object by using a foreach loop:
foreach ($objArr as $obj) { echo 'Name: ' . $obj->name . ', Age: ' . $obj->age . '<br>'; }
In the above code, we use a foreach loop to iterate through each stdClass object in the $objArr variable, and then use the object's property name to get the object attribute value. Therefore, we can access the data in the array object in the same way as a normal array.
- Summary
This article describes how to convert an ordinary array in PHP into an array of array objects. We used the array_map() function to achieve this. By using the stdClass class to instantiate a new array object and assigning the key-value pairs in the original array to the new instance, we finally get an array of array objects. Such a data structure can better organize data and facilitate use in various scenarios.
The above is the detailed content of How to convert array to array object in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
