Home  >  Article  >  Backend Development  >  How to convert json string to multidimensional array in PHP

How to convert json string to multidimensional array in PHP

PHPz
PHPzOriginal
2023-04-21 09:13:58966browse

In the PHP development process, data storage and transmission are very important, and the use of json strings has become very common. Use json to convert multi-dimensional arrays into string format, which is widely used in various applications. This article mainly introduces how to convert json string into multi-dimensional array in PHP.

  1. json_decode function

PHP provides the json_decode function for converting json strings into multi-dimensional array format. This function accepts two parameters. The first parameter is the json string that needs to be converted. The second parameter is optional and is a Boolean parameter used to set whether to convert the json string into an associative array (default is false) .

For example:

$a = '{"name":"Tom","age":20,"is_student":true}';
$b = json_decode($a);
var_dump($b); // 输出:object(stdClass)#1 (3) { ["name"]=> string(3) "Tom" ["age"]=> int(20) ["is_student"]=> bool(true) }

In the above example, the original json string is converted into a stdClass object. The attributes and attribute values ​​in the object are the keys and values ​​in json respectively.

Since the json_decode function converts the json string into a stdClass object by default, you need to pay attention when using it. When accessing attributes, you need to use the "->" symbol instead of the array subscript form.

For example:

$a = '{"name":"Tom","age":20,"is_student":true}';
$b = json_decode($a);
echo $b->name; // 输出:Tom
  1. json_decode function and array

If you want to convert the json string into array format, you can call the json_decode function with The second parameter is set to true.

For example:

$a = '{"name":"Tom","age":20,"is_student":true}';
$b = json_decode($a,true);
var_dump($b);  // 输出:array(3) { ["name"]=> string(3) "Tom" ["age"]=> int(20) ["is_student"]=> bool(true) }

In this way, you can use the subscript form when accessing the array.

For example:

$a = '{"name":"Tom","age":20,"is_student":true}';
$b = json_decode($a,true);
echo $b['name']; // 输出:Tom
  1. Use the json_last_error function

When using the json_decode function, if the json string format is incorrect, it will cause a parsing error. At this time, you can use the json_last_error function to view the errors that occurred during the latest parsing so that the problem can be repaired in time.

For example:

$a = '{"name":"Tom","age:20,"is_student":true}';  // json格式不正确,男少了一个引号
$b = json_decode($a);
if(json_last_error() !== JSON_ERROR_NONE){
    echo "解析错误:" . json_last_error_msg();
}

In the above code, due to the incorrect json string format causing a parsing error, the json_last_error function returned JSON_ERROR_SYNTAX, and then used the json_last_error_msg function to output the error message.

  1. Summary

As can be seen from the above introduction, PHP provides the json_decode function to facilitate us to convert json strings into multi-dimensional arrays. When using it, you need to pay attention to setting the second parameter to true to facilitate array operations; at the same time, you should use the json_last_error function to check the json string format to avoid parsing errors. Using these methods, you can easily process data in json format.

The above is the detailed content of How to convert json string to multidimensional 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