What are the common ways to convert arrays to objects in PHP?

WBOY
Release: 2024-04-28 22:54:02
Original
704 people have browsed it

How to convert PHP array to object: use stdClass class, use json_decode() function, use third-party library (such as ArrayObject class, Hydrator library)

PHP 数组转对象的常见方式有哪些?

PHP array Common ways to convert an object

In PHP, there are several ways to convert an array into an object. The following are some common methods:

1. Use thestdClassclass

stdClassclass is a standard class provided by PHP , can be used to create an empty object. We can use the properties of thestdClassobject to store key-value pairs in an array.

$array = ['name' => 'John Doe', 'age' => 30]; $object = new stdClass(); foreach ($array as $key => $value) { $object->$key = $value; }
Copy after login

2. Use the built-in functionjson_decode()

json_decode()The function can decode the JSON string into PHP object. We can convert the array to a JSON string and then decode it into an object using thejson_decode()function.

$array = ['name' => 'John Doe', 'age' => 30]; $json = json_encode($array); $object = json_decode($json);
Copy after login

3. Use third-party libraries

There are some third-party libraries that can also be used for conversion of arrays and objects, for example:

  • ArrayObjectClass ([PHP Documentation](https://www.php.net/manual/en/class.arrayobject.php))
  • HydratorLibrary ([Composer](https://packagist.org/packages/laminas/laminas-hydrator))

Practical case

Suppose we have a package containing Array of user data:

$users = [ ['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com'], ['id' => 2, 'name' => 'Jane Doe', 'email' => 'jane@example.com'], ];
Copy after login

We can convert the array to object using the above method:

UsingstdClassClass:

foreach ($users as $user) { $object = new stdClass(); $object->id = $user['id']; $object->name = $user['name']; $object->email = $user['email']; }
Copy after login

Usejson_decode()Function:

foreach ($users as $user) { $json = json_encode($user); $object = json_decode($json); }
Copy after login

UseArrayObjectClass:

foreach ($users as $user) { $object = new ArrayObject($user); }
Copy after login

Now we have a collection of objects containing user data and we can easily access their properties. For example:

echo $object->name; // 输出:"John Doe"
Copy after login

The above is the detailed content of What are the common ways to convert arrays to objects 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
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!