This article mainly introduces the usage of phpjson-related functions, lists the functions of json_encode, json_decode, and json_last_error, and analyzes the specific usage skills of json_encode and json_decode functions, for more information about the usage of json-related php functions, see this article. the functions of json_encode, json_decode, and json_last_error are listed. the usage skills of json_encode and json_decode functions are analyzed, for more information, see
This example describes the usage of php json-related functions. We will share this with you for your reference. The details are as follows:
Function list:
Function |
Description |
Json_encode |
Encode variables in json format |
Json_decode |
Decodes json strings and converts them to php variables. |
Json_last_error |
Returns the last error. |
For example 1: Json_encode
$arr=array("A"=>"a","B"=>"b","C"=>"c","D"=>"d");echo json_encode($arr);
Output:
{"A":"a","B":"b","C":"c","D":"d"}
For example 2: Json_decode
$arr='{"A":"a","B":"b","C":"c","D":"d"}';var_dump(json_decode($arr));var_dump(json_decode($arr,true));
Output:
object(stdClass)[1] public 'A' => string 'a' (length=1) public 'B' => string 'b' (length=1) public 'C' => string 'c' (length=1) public 'D' => string 'd' (length=1)array (size=4) 'A' => string 'a' (length=1) 'B' => string 'b' (length=1) 'C' => string 'c' (length=1) 'D' => string 'd' (length=1)
The above is a detailed explanation of the usage examples of php json-related functions. For more information, see other related articles in the first PHP community!