Home > Backend Development > PHP Problem > How to convert array into object in php

How to convert array into object in php

PHPz
Release: 2023-05-19 09:06:07
Original
1630 people have browsed it

In PHP, arrays and objects are two common data types, and each data type has its own uses and limitations. Sometimes, it is necessary to convert between these two data types. This article will discuss how to convert an array into an object.

In PHP, you can use type conversion or cast syntax to convert an array into an object. The following are two common methods:

Method 1: Use (object) cast conversion

In PHP, you can use (object) cast syntax to convert an array into an object. For example:

$arr = array('name' => 'Tom', 'age' => 20);
$obj = (object) $arr;
Copy after login

The above code converts the $arr array into an $obj object.

It should be noted that when using (object) cast syntax, the array key name becomes the object attribute name, and the array key value becomes the object attribute value. If the original array contains a multidimensional array, it will be converted into a nested object.

For example:

$arr = array(
   'name' => 'Tom',
   'age' => 20,
   'contact' => array(
      'email' => 'tom@example.com',
      'phone' => '1234567890'
   )
);

$obj = (object) $arr;
Copy after login

In the above code, the $obj object contains all the data in the $arr array, and the contact array is converted into a nested object.

Method 2: Use the json_decode() function

In addition to using the cast syntax, you can also use the json_decode() function to convert the array into an object.

First, you need to convert the array into a string in JSON format, and then use the json_decode() function to convert it into an object. For example:

$arr = array('name' => 'Tom', 'age' => 20);
$json = json_encode($arr);
$obj = json_decode($json);
Copy after login

In the above code, the $json variable contains the JSON string converted from the $arr array, and the $obj variable is the object converted from the $json string through the json_decode() function.

It should be noted that when using the json_decode() function to convert, by default, the returned object is a stdClass object, not a custom class. If you want to convert it into a custom class, you can pass additional parameters to the json_decode() function.

For example:

class Person {
   public $name;
   public $age;
}

$arr = array('name' => 'Tom', 'age' => 20);
$json = json_encode($arr);
$obj = json_decode($json, false, 512, JSON_OBJECT_AS_ARRAY);
$obj = (object) $obj;

$person = new Person();
$person->name = $obj->name;
$person->age = $obj->age;
Copy after login

In the above code, the JSON_OBJECT_AS_ARRAY parameter is used to tell the json_decode() function to return an array instead of a stdClass object. Then, use cast syntax to convert the array into an object.

Finally, we can instantiate the Person object by assigning the property value to the member variable of the custom class.

Summary

This article introduces two common methods of converting arrays into objects in PHP: using cast syntax and using the json_decode() function. No matter which method is used, you can easily convert between arrays and objects to meet the needs of different scenarios.

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