Home > Article > Web Front-end > How to use json_encode function to convert specified value to JSON format
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 ]] )".
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 ]] )
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;
Execution results
{"apple":"fruits1","orange":"fruits2","melon":"fruits3","pineapple":"fruits4","Strawberry":"fruits5"}
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!