Home > Web Front-end > JS Tutorial > How to use json_encode function to convert specified value to JSON format

How to use json_encode function to convert specified value to JSON format

不言
Release: 2021-04-22 17:50:10
Original
4423 people have browsed it

The method to use the json_encode function to convert the specified value into JSON format is to convert it through the syntax "string json_encode( $value [, JSON constant [, depth = 512 ]] )".

How to use json_encode function to convert specified value to JSON format

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer.

The json_encode function can convert the specified value into JSON format, so in this article we will introduce to you the specific method of using the json_encode function to convert the specified value into JSON format.

First let’s take a look at the json_encode function description

string json_encode( $值 [, JSON常数 [, 深度 = 512 ]] )
Copy after login

Parameters:

The first parameter specifies the string or array used to convert to JSON format .

If you specify the second parameter, you can specify the format to be converted and optionally specify a constant.

When specifying the third parameter, the maximum depth can be specified as a positive number.

Return value:

The json_encode function returns the JSON encoded value as the return value, or false if the process fails.

Note: All string data to be converted must be encoded with character code UTR-8.

Next let’s take a lookHow to use the json_encode function

Let’s look at a specific example

$fruits_array = ['apple'=>'fruits1',
                 'orange'=>'fruits2',
                 'melon'=>'fruits3',
                 'pineapple'=>'fruits4',
                 'Strawberry'=>'fruits5'
                ];
$jsonstr =  json_encode($fruits_array);
echo $jsonstr;
Copy after login

Execution results

{"apple":"fruits1","orange":"fruits2","melon":"fruits3","pineapple":"fruits4","Strawberry":"fruits5"}
Copy after login

View the execution results. There is a colon ":" between strings, and each is separated by a comma ",".

This article ends here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use json_encode function to convert specified value to JSON format. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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