Home  >  Article  >  Backend Development  >  Summarize the conversion methods between string and JSON format in php

Summarize the conversion methods between string and JSON format in php

PHPz
PHPzOriginal
2023-04-03 15:00:561077browse

In PHP, we often need to convert strings into JSON format. JSON is a lightweight data exchange format that is widely used in web development. This article will introduce how to use PHP to convert strings into JSON format.

PHP provides a built-in function json_encode(), which can convert PHP objects, arrays, and other values ​​into JSON format strings. The following is a sample code:

$data = array(
    'name' => 'Tom',
    'age' => 25,
    'hobbies' => array('reading', 'traveling', 'music')
);

$json = json_encode($data);
echo $json;

This code converts an array containing name, age and interests into a string in JSON format and outputs it. The output is as follows:

{"name":"Tom","age":25,"hobbies":["reading","traveling","music"]}

As you can see, the JSON string uses double quotes to contain the attribute name and string value, and uses single quotes to contain the array value. Note that JSON format requires that attribute names must be enclosed in double quotes, and single quotes are not allowed.

Sometimes, we need to convert a string that already contains JSON format back to a PHP array or object. PHP provides a built-in function json_decode(), which can convert JSON-formatted strings into PHP arrays or objects. The following is a sample code:

$json = '{"name":"Tom","age":25,"hobbies":["reading","traveling","music"]}';
$data = json_decode($json);
print_r($data);

This code converts a JSON format string into a PHP array and prints the output. The output is as follows:

stdClass Object
(
    [name] => Tom
    [age] => 25
    [hobbies] => Array
        (
            [0] => reading
            [1] => traveling
            [2] => music
        )

)

As you can see, the JSON format string is converted into a PHP object containing attributes and attribute values.

In addition, the json_encode() and json_decode() functions also provide some optional parameters to control the formatting of the JSON string and the decoded PHP object type. If you need more details, check out the official PHP documentation.

To sum up, PHP's built-in json_encode() and json_decode() functions are important tools for converting strings into JSON format and converting JSON format strings back to PHP arrays or objects. Mastering the use of these functions can play an important role in web development.

The above is the detailed content of Summarize the conversion methods between string and JSON format in php. 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