How to convert array to JSON format using PHP

PHPz
Release: 2023-04-24 15:40:14
Original
450 people have browsed it

PHP is a very popular web development language, and JSON is a very commonly used data format in web development. Therefore, converting PHP arrays to JSON format is a very important skill. In this article, we will explain how to convert an array to JSON format using PHP.

Step 1: Create a PHP array

Before converting the PHP array to JSON format, you first need to create a PHP array. The following is a simple PHP array example:

$students = array(
  array("name" => "Tom", "age" => 20, "grade" => 85),
  array("name" => "Jerry", "age" => 21, "grade" => 90),
  array("name" => "Mickey", "age" => 22, "grade" => 95)
);
Copy after login

This PHP array contains information about three students, each student has a name, an age and a grade.

Step 2: Convert a PHP array to JSON format using json_encode

The easiest way to convert a PHP array to JSON format is to use PHP's built-in json_encode function. The following is an example of converting a PHP array to JSON format:

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

In this example, we use the json_encode function to convert the $students array to JSON format, and use the echo statement to print the array in JSON format. The following is the output result:

[
  {"name":"Tom","age":20,"grade":85},
  {"name":"Jerry","age":21,"grade":90},
  {"name":"Mickey","age":22,"grade":95}
]
Copy after login

As you can see, the output result is an array in JSON format, and each student's information is converted into a JSON object.

Step 3: Convert JSON format to PHP array using json_decode

In addition to converting PHP array to JSON format, we sometimes need to convert JSON format to PHP array. This can be achieved by using PHP's built-in json_decode function. Here is an example of converting JSON format to a PHP array:

$json = '[{"name":"Tom","age":20,"grade":85},{"name":"Jerry","age":21,"grade":90},{"name":"Mickey","age":22,"grade":95}]';
$students = json_decode($json, true);
print_r($students);
Copy after login

In this example, we first define a JSON format string and then use the json_decode function to convert it to a PHP array. It should be noted that the second parameter of the json_decode function is passed in a Boolean value true, which means that the returned result will be an associative array, not an object. Finally, we print the contents of the PHP array using the print_r function. The following is the output result:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 20
            [grade] => 85
        )

    [1] => Array
        (
            [name] => Jerry
            [age] => 21
            [grade] => 90
        )

    [2] => Array
        (
            [name] => Mickey
            [age] => 22
            [grade] => 95
        )

)
Copy after login

As you can see, the output result is an associative array, which contains the information of each student.

Summary

In this article, we introduced how to convert PHP array to JSON format, and how to convert JSON format to PHP array. This is a very common technique in web development, I hope this article will be helpful to you.

The above is the detailed content of How to convert array to JSON format using 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