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

PHPz
Release: 2023-04-20 11:24:59
Original
804 people have browsed it

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!

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