Home  >  Article  >  Backend Development  >  json to array php

json to array php

WBOY
WBOYOriginal
2023-05-22 19:23:091303browse

In the process of web development, we often need to convert JSON data format into arrays. The PHP language has a good ability to process JSON and can quickly convert data in JSON format into an array.

In PHP, there are built-in json_decode() function and json_encode() function to convert between JSON and arrays. Below we will introduce how to use these two functions respectively.

1. json_decode() function

json_decode() function is used to convert JSON data format into PHP array. Its syntax is as follows:

mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )

Among them, the json parameter is the JSON string to be converted; the assoc parameter is used to specify whether the return value is an object or an array, and the default is an array; the depth parameter is used to set the maximum nesting depth , the default value is 512; the options parameter is used to set encoding options and is usually not needed.

The following is an example of converting a JSON string into an array:

$json_str = '{"name":"Tom","age":18}';
$arr = json_decode($json_str, true);
print_r($arr);

The output of the above code is:

Array (
    [name] => Tom
    [age] => 18
)

2. json_encode() function

# The ##json_encode() function is used to convert a PHP array into a JSON format string. Its syntax is as follows:

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

Among them, the value parameter is the array or object to be converted; the options parameter is used to set encoding options, which is usually not needed; the depth parameter is used to set the maximum nesting depth, and the default value is 512.

The following is an example of converting an array into a JSON string:

$arr = array('name' => 'Tom', 'age' => 18);
$json_str = json_encode($arr);
echo $json_str;

The output of the above code is:

{"name":"Tom","age":18}

3. Complete example

The following is a complete example, converting a JSON array containing multiple elements into a PHP array and outputting one of the elements:

$json_str = '[{"name":"Tom","age":18},{"name":"Jack","age":20},{"name":"Mary","age":22}]';
$arr = json_decode($json_str, true);
echo $arr[1]['name'];

The output of the above code is:

Jack

Summary: The json_decode() function and json_encode() function are very practical and can effectively convert between JSON and arrays. In actual development, we often need to convert JSON data format into an array or convert an array into a JSON format string. These two functions can help us. It should be noted that the json_decode() function can only convert standard JSON format strings, otherwise an error will occur.

The above is the detailed content of json to array 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
Previous article:PHP query array subscriptNext article:PHP query array subscript