Home  >  Article  >  Backend Development  >  JSON function for PHP function

JSON function for PHP function

WBOY
WBOYOriginal
2023-05-19 14:51:061589browse

PHP is a highly powerful language that can not only handle requests from the front end, but also communicate with the back end. In this regard, the use of JSON functions in PHP is very important.

JSON (JavaScript Object Notation) is a lightweight data format that uses a compact text format to transmit data. In the front end, JavaScript objects and JSON objects can be easily converted to each other. In PHP, we can use JSON functions to communicate with other programming languages.

The following will introduce some commonly used JSON functions in PHP:

  1. json_encode() function

json_encode() function converts PHP arrays into json characters string. For example:

$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

echo json_encode($data);

This will output the following JSON string:

{"name":"John Doe","age":30,"city":"New York"}
  1. json_decode() function

json_decode() function converts the JSON string to PHP array. For example:

$json_string = '{"name":"John Doe","age":30,"city":"New York"}';

$data = json_decode($json_string, true);

echo $data['name']; // 输出 John Doe

Note that the second parameter is set to true, which causes the json_decode() function to return an associative array, whereas by default it returns an object.

  1. json_last_error() function

json_last_error() function returns the error code of the most recent JSON encoding or decoding operation. For example:

$json_string = 'invalid_json_string';

$data = json_decode($json_string, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'Error: ' . json_last_error_msg();
}

Output:

Error: Syntax error
  1. json_last_error_msg() function

json_last_error_msg() function returns the latest error message of the JSON encoding or decoding operation. For example:

$json_string = 'invalid_json_string';

$data = json_decode($json_string, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'Error: ' . json_last_error_msg();
}

Output:

Error: Syntax error
  1. Options in json_encode()

json_encode() function can accept a second parameter to set encoding options . For example:

$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

$options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE;

echo json_encode($data, $options);

This will output the following JSON string:

{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

Among them, the JSON_PRETTY_PRINT option will output beautiful-formatted JSON, and the JSON_UNESCAPED_UNICODE option will not escape Unicode characters.

Summary: In PHP, JSON functions are very important. It allows us to convert PHP arrays to json strings and also allows us to convert json strings to PHP arrays. Additionally, we can use other JSON functions to check for errors and set encoding options.

The above is the detailed content of JSON function for PHP function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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