What is the function to convert php array to josn

PHPz
Release: 2023-04-26 14:07:33
Original
491 people have browsed it

In PHP, we often need to convert arrays into JSON format strings to facilitate data transfer between different applications. PHP provides a built-in functionjson_encode()to implement this function. Let's take a look at the usage of this function.

json_encode()The function can convert a PHP array into a JSON string. The syntax is as follows:

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

Parameter description:

  • $value: The value that needs to be encoded can be a value, array or object.
  • $options: Optional parameters, used to set encoding options, affecting the encoding processing method. The default is 0, which means no special options are used.
  • $depth: Optional parameter used to set the maximum recursion depth. The default is 512, which means the depth does not exceed 512.

Sample code:

 'red', 'banana' => 'yellow', 'orange' => 'orange' ); // 将PHP数组转换为JSON格式字符串 $json = json_encode($fruits); // 输出JSON字符串 echo $json; ?>
Copy after login

Output result:

{"apple":"red","banana":"yellow","orange":"orange"}
Copy after login

As shown in the above code, we first define a PHP associative array$fruits, which contains several fruits and their colors. Then use thejson_encode()function to convert the array into a JSON string and output the result.

As can be seen from the output, the converted JSON string has the same structure as the original PHP array, except that each key-value pair is enclosed in double quotes and separated by commas.

In actual applications, we may need to further process the JSON string, such as parsing it to obtain the data. PHP provides a built-in functionjson_decode()to parse JSON strings.

Sample code:

         
Copy after login

Output result:

array(3) { ["apple"]=> string(3) "red" ["banana"]=> string(6) "yellow" ["orange"]=> string(6) "orange" }
Copy after login

As shown in the above code, we first define a JSON string$json_str, Several fruits and their colors are included. Then use thejson_decode()function to parse the JSON string into a PHP associative array and output the array.

As can be seen from the output results, the parsed PHP array has the same structure as the original JSON string, except that the key-value pairs are connected using the=>symbol. It should be noted that the second parameter of thejson_decode()function can be used to control whether the decoding result should be converted into an associative array. Iftrueis passed in, the decoding result will be converted into an associative array. It is an associative array, otherwise it will be converted into a PHP object.

The above is the detailed content of What is the function to convert php array to josn. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!