Detailed explanation of how to convert data to json format in php

PHPz
Release: 2023-04-04 21:42:01
Original
2831 people have browsed it

With the rapid development of Internet technology, more and more websites and applications use ajax technology to realize data interaction with the server. As one of the most commonly used data exchange formats, json has become the first choice of many developers. How to convert data into json format in php, this is what this article will introduce.

1. Introduction to json format

JSON stands for JavaScript Object Notation. It is a lightweight data format that is easy to read and write. It is based on a subset of the JavaScript language and therefore can be supported by various languages. Compared with xml format, json format is more concise, lightweight, and has higher parsing speed. In web applications, json is widely used in data interaction, such as ajax requests, API interfaces, etc.

2. PHP to json function

In PHP, you can use the json_encode() function to convert data into json format. This function can convert any php data type into json format. For example:

$data = array(
    'name' => 'Tom',
    'age'  => 20,
    'sex'  => 'Male'
);

$json = json_encode($data);
echo $json;
Copy after login

In the above example, we define an array $data, including name, age and sex fields. Then use the json_encode() function to convert the array into a json string and store it in the variable $json. Finally, the string is output through the echo statement. The output result is as follows:

{"name":"Tom","age":20,"sex":"Male"}
Copy after login

3. Convert the request result into json format

In practical applications, we usually need to convert the results returned by the server into json format to facilitate the client's parsing and processing. For example, how to convert database query results into json format in php?

Suppose we need to query the data of a student table. The query results are as follows:

+----+--------+------+------+--------+
| id | name   | age  | sex  | grades |
+----+--------+------+------+--------+
|  1 | Tom    |   20 | Male |     85 |
|  2 | Jack   |   18 | Male |     92 |
|  3 | Alice  |   19 | Female |     78 |
+----+--------+------+------+--------+
Copy after login

We can use PHP’s mysqli extension to perform query operations, for example:

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM students";
$result = $conn->query($sql);
Copy after login

Then we need To convert the query results into json format, you can use the following method:

$rows = array();
while ($row = $result->fetch_assoc()) {
    $rows[] = $row;
}

$json = json_encode($rows);
echo $json;
Copy after login

Among them, $result is the query result set object, and the fetch_assoc() method can be used to call a row of query results to convert into an associative array. Then we can store the associative array of each row into the $rows array, and finally convert the $rows array into a json string through the json_encode() function and output it to the client.

The output results are as follows:

[
    {
        "id": "1",
        "name": "Tom",
        "age": "20",
        "sex": "Male",
        "grades": "85"
    },
    {
        "id": "2",
        "name": "Jack",
        "age": "18",
        "sex": "Male",
        "grades": "92"
    },
    {
        "id": "3",
        "name": "Alice",
        "age": "19",
        "sex": "Female",
        "grades": "78"
    }
]
Copy after login

4. Notes

Although the json_encode() function is a built-in function in PHP, you need to pay attention to the following points when using this function:

  1. This function requires the support of php5 or above;
  2. If your json string contains Chinese characters, it will not be displayed normally. This can be solved by adding the JSON_UNESCAPED_UNICODE parameter.
  3. If your data has special characters such as line breaks and tabs, you need to use the JSON_PRETTY_PRINT parameter to beautify the json format.

5. Summary

php is a very popular server-side scripting language. In the process of data interaction, it is more convenient to transmit data in json format. Whether you are getting data from the database or receiving a client request, you can use the json_encode() function to convert the data into json format to facilitate client parsing and processing. In applications, we need to pay attention to issues such as the setting of the output character set and the escaping of special characters to ensure the reliability and correctness of the data.

The above is the detailed content of Detailed explanation of how to convert data to json format in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!