Home Backend Development PHP Problem How to convert json string to array or object in php

How to convert json string to array or object in php

Apr 20, 2023 am 10:12 AM

When using PHP for web development, we often deal with JSON data. If the data we receive is JSON data in the form of a string, then we need to convert it into an object or array in PHP for subsequent processing. In this article, we will discuss how to convert JSON data in the form of strings to objects or arrays.

1. Convert a string to a JSON object

First, we need to use the json_decode() function in PHP to convert a JSON string to a JSON object. The syntax of this function is as follows:

json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0): mixed
Copy after login

Among them, the $json parameter indicates the JSON string that needs to be converted; the $assoc parameter indicates whether to convert the converted JSON object. It is in the form of an array; the $depth parameter represents the maximum recursion depth; the $options parameter represents the options during conversion.

The sample code is as follows:

$json_string = '{"name":"张三","age":18}';
$json_obj = json_decode($json_string);
var_dump($json_obj);
Copy after login

Output result:

object(stdClass)#1 (2) {
  ["name"]=>
  string(6) "张三"
  ["age"]=>
  int(18)
}
Copy after login

As mentioned above, use the json_decode() function to convert the JSON string into a JSON object , returns the stdClass object in PHP.

2. Convert the string to a JSON array

If we need to convert the JSON string to an array in PHP, we can set the $assoc parameter to true. The sample code is as follows:

$json_string = '[{"name":"张三","age":18},{"name":"李四","age":20}]';
$json_arr = json_decode($json_string, true);
var_dump($json_arr);
Copy after login

Output result:

array(2) {
  [0]=>
  array(2) {
    ["name"]=>
    string(6) "张三"
    ["age"]=>
    int(18)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(6) "李四"
    ["age"]=>
    int(20)
  }
}
Copy after login

As can be seen from the above example, after conversion, the data is originally a JSON object, such as {"name":" Zhang San", "age":18}, is converted into an array form in PHP, such as ["name" => "Zhang San", "age" => 18] .

3. Convert a string to an array of JSON objects

In practical applications, we may need to convert a JSON string composed of multiple JSON objects into an array of objects in PHP.

The sample code is as follows:

$json_string = '[{"name":"张三","age":18},{"name":"李四","age":20}]';
$json_obj_arr = json_decode($json_string);
var_dump($json_obj_arr);
Copy after login

Output result:

array(2) {
  [0]=>
  object(stdClass)#2 (2) {
    ["name"]=>
    string(6) "张三"
    ["age"]=>
    int(18)
  }
  [1]=>
  object(stdClass)#3 (2) {
    ["name"]=>
    string(6) "李四"
    ["age"]=>
    int(20)
  }
}
Copy after login

Compared with converting a JSON string to an array, converting it to an object array is a more complicated operation. In this sample code, the json_decode() function is used to convert a string to an array of JSON objects. The return result is an array consisting of multiple stdClass objects.

To sum up, processing JSON data is an essential part of the web development process. Through the introduction of this article, you have learned how to convert a JSON string to an object or array in PHP, and how to convert a JSON string consisting of multiple objects to an array of objects in PHP.

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24