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

How to convert json into object array in php

PHPz
PHPzOriginal
2023-04-19 09:20:28839browse

In PHP development, JSON (JavaScript Object Notation) is a common data exchange format. Converting JSON data into an object array is one of the basic tasks that PHP developers often need to deal with. This article will introduce how to convert JSON into an object array, hoping to provide some help to PHP developers.

1. Prerequisite knowledge

Before learning to convert JSON into an object array, we need to understand the following concepts.

  1. JSON: JavaScript Object Notation, a lightweight data exchange format that is easy to read and write.
  2. json_encode() function: PHP built-in function, converts PHP variables into JSON data format.
  3. json_decode() function: PHP built-in function, converts JSON data into PHP variables.

2. Method to convert JSON into an object array

1. Use json_decode() function

json_decode() function is PHP’s built-in function for parsing JSON . Through the json_decode() function, we can convert JSON data into PHP object or array form. Below is sample code.

$json_str = '{"name":"John", "age":30, "isMarried":true}';
$obj = json_decode($json_str);
echo $obj->name; // Output: John

Parsing process:

  • Define a JSON string.
  • Use the json_decode() function to convert a JSON string into a PHP object.
  • Apply the attributes in the object, and the output result is "John".

2. Convert JSON into an associative array

Convert JSON into an associative array, similar to the above method, just set the second parameter of the json_decode() function to Just be true. Below is sample code.

$json_str = '{"name":"John", "age":30, "isMarried":true}';
$arr = json_decode($json_str, true);
echo $arr['name']; // Output: John

Parsing process:

  • Define a JSON string.
  • Use the json_decode() function to convert a JSON string into a PHP associative array.
  • Apply the elements in the array, and the output result is "John".

It should be noted that when using this method, the attribute names in the JSON data will become the key names in the array.

3. Application of converting JSON into object array

The above are the basic methods and principles for converting JSON into object array. Some of these practical application scenarios are described below.

1. Obtain JSON data through API and process it

In web development, it is often necessary to obtain JSON data through API. The obtained JSON data can be converted into a PHP object or array through the json_decode() function and then processed. Below is sample code.

$url = 'http://example.com/api/data.json';
$data = file_get_contents($url);
$obj = json_decode($data);
echo $obj->name;

Parsing process:

  • Define the URL address of the API.
  • Use the file_get_contents() function to obtain the JSON data returned by the API.
  • Use the json_decode() function to convert JSON data into PHP objects.
  • Apply the attributes in the object and output the result.

2. Convert the JSON data submitted by the user into an object array

In web applications, sometimes users submit JSON data. You can use the json_decode() function to convert the JSON data submitted by the user into a PHP object or array to facilitate subsequent processing. Below is sample code.

$json_str = '{"name":"John", "age":30, "isMarried":true}';
$obj = json_decode($json_str);
echo $obj->name;

Parsing process:

  • Define a JSON string, which is submitted by the user.
  • Use the json_decode() function to convert a JSON string into a PHP object.
  • Apply the attributes in the object and output the result.

3. Convert JSON data read from the database into an object array

In web development, data is usually stored in the database. If some data is stored in the database in JSON format, you can use the json_decode() function to convert it into a PHP object or array to facilitate subsequent processing. Below is sample code.

$query = "SELECT data FROM tbl WHERE id=1";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
$obj = json_decode($row['data']);
echo $obj->name;

Parsing process:

  • Execute a SQL query statement to obtain a piece of data.
  • Use the mysqli_fetch_array() function to obtain query results.
  • Use the json_decode() function to convert the JSON data in the query results into a PHP object.
  • Apply the attributes in the object and output the result.

4. Summary

This article introduces the method, application scenarios and some details of converting JSON into an object array. I hope this article can be helpful to PHP developers and make development work more convenient and efficient.

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