Home>Article>Backend Development> How to convert array to object in php

How to convert array to object in php

PHPz
PHPz Original
2023-03-23 15:51:27 2138browse

In the PHP language, array and object are two common data types. Although arrays are typically used to store large amounts of data, there may be situations where you need to convert an array to an object. This article will introduce how to convert array to object in PHP.

Two methods to convert array to object in PHP

  • Method 1: Use forced type conversion

In PHP, you can use cast to convert array into object. This process is very simple, just add (object) before the array variable.

The sample code is as follows:

$array = array('name' => 'John', 'age' => 25); $object = (object)$array;

In the above example, $array is an array containing two elements, which contains the two keys "name" and "age" and the corresponding values. . By using cast, you can create a new object $object with the same data.

In this case, $object will actually be a stdClass object, because in PHP, stdClass is a typed empty object that can be used to hold dynamic properties. To access data from an object, use the object operator ( -> ).

echo $object->name; //将输出 'John' echo $object->age; //将输出 25
  • Method 2: Use json_decode() function

Another method to convert array to object is to use json_decode() in PHP function. This function is typically used to decode JSON-formatted strings, but can also be used to convert an array to an object.

The sample code is as follows:

$array = array('name' => 'John', 'age' => 25); $objJSON = json_encode($array); $object = json_decode($objJSON);

In this example, use the json_encode() function to convert the array into a JSON string, and then use the json_decode() function to decode the JSON string into a new object $object.

As with the first method, in this case you can use the object operator ( -> ) to access the data.

echo $object->name; //将输出 'John' echo $object->age; //将输出 25

Notes

There are some important things to note when using these methods.

First, if the converted array contains nested arrays, you must first convert the nested arrays to objects, and then convert the entire array to objects.

Secondly, in cast, it will only work properly if the key name is a valid PHP variable name (for example, does not contain spaces or other illegal characters).

Finally, these methods do not do any sorting of the keys of the array, so the order of the elements may change after conversion to an object.

Summary

In PHP, converting array to object is a very useful skill. This task can be accomplished using casts or the json_decode() function. Use these methods to easily access data in an array and take full advantage of the advantages and capabilities of objects in PHP.

The above is the detailed content of How to convert array to object in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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