Home >Backend Development >PHP Tutorial >What are the common JSON operations in PHP programming?
PHP is a commonly used programming language that is widely used for website development and data processing. In PHP development, JSON is a very important data format used to transfer data between different systems and programs. PHP provides a series of JSON operation functions. This article will introduce the common functions and usage.
json_encode() function converts a PHP array or object into a JSON format string. This function takes two parameters: the first parameter is the PHP array or object to be converted, and the second parameter is an option that can be used to control the format of the JSON output. For example:
$data = array('name' => 'Tom', 'age' => 25); $json = json_encode($data); echo $json;
Output result:
{"name":"Tom","age":25}
json_decode() function converts a JSON-formatted string into a PHP array or object . This function takes two parameters: the first parameter is the JSON string to be converted, and the second parameter is a Boolean value used to specify whether the return value is an array or an object. For example:
$json = '{"name":"Tom","age":25}'; $data = json_decode($json); print_r($data);
Output result:
stdClass Object ( [name] => Tom [age] => 25 )
json_last_error() function returns the error code that occurred during the last JSON encoding and decoding process . If no error occurs, it returns 0. For example:
$json = '{"name":"Tom","age":25}'; $data = json_decode($json); if (json_last_error() != 0) { echo 'JSON decoding error: ' . json_last_error_msg(); }
json_last_error_msg() function returns the error message that occurred during the last JSON encoding and decoding process. For example:
$json = '{name:"Tom",age:25}'; $data = json_decode($json); if (json_last_error() != 0) { echo 'JSON decoding error: ' . json_last_error_msg(); }
Output result:
JSON decoding error: Syntax error
json_encode_options() is an option constant that can be used to control json_encode() The JSON output format generated by the function. For example:
JSON_UNESCAPED_UNICODE
option is used to retain the original encoding of Chinese characters instead of converting them to Unicode encoding.
$data = array('name' => '汤姆', 'age' => 25); $json = json_encode($data, JSON_UNESCAPED_UNICODE); echo $json;
Output results:
{"name":"汤姆","age":25}
JSON_PRETTY_PRINT
option is used to format the JSON output to make it easier to read and debug.
$data = array('name' => 'Tom', 'age' => 25); $json = json_encode($data, JSON_PRETTY_PRINT); echo $json;
Output result:
{ "name": "Tom", "age": 25 }
To sum up, the above are the common JSON operation functions and options in PHP. Mastering these functions and options can make it easier to encode and decode JSON in PHP development and improve development efficiency.
The above is the detailed content of What are the common JSON operations in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!