Home > Backend Development > PHP Problem > How to convert two-dimensional array to json in php

How to convert two-dimensional array to json in php

PHPz
Release: 2023-04-18 13:40:02
Original
701 people have browsed it

In PHP, it is very common to use two-dimensional arrays to store various data. It is very important to convert these two-dimensional arrays into JSON format data. Because in today's Internet era, JSON format has become the standard format for data exchange. Transmitting data through JSON format can make the data clearer and not limited by language. Below, we will introduce how to convert a two-dimensional array in PHP into JSON format data.

First, let’s take a look at the simplest method:

$array = array(
    array("name" => "小明", "age" => 18),
    array("name" => "小红", "age" => 20)
);
$json = json_encode($array);
echo $json;
Copy after login

In the above code, we define a two-dimensional array and then use the json_encode function to convert it into JSON format data and output the results.

At this point, we will get the following result:

[{"name":"小明","age":18},{"name":"小红","age":20}]
Copy after login

You can see that the two-dimensional array has been successfully converted into JSON format data.

Let’s look at a slightly more complex example. We will define a two-dimensional array containing multiple attributes for demonstration. For example:

$array = array(
    array("id" => 1, "name" => "小明", "age" => 18, "gender" => "男"),
    array("id" => 2, "name" => "小红", "age" => 20, "gender" => "女"),
    array("id" => 3, "name" => "小刚", "age" => 19, "gender" => "男")
);
Copy after login

We can convert this two-dimensional array into JSON format data by looping through it. For example:

$json = "[";
foreach ($array as $key => $value) {
    $json .= "{";
    foreach ($value as $k => $v) {
        $json .= "\"" . $k . "\":\"" . $v . "\",";
    }
    $json = rtrim($json, ",");
    $json .= "},";
}
$json = rtrim($json, ",");
$json .= "]";
echo $json;
Copy after login

The implementation principle of this code is as follows: first define a $json variable to store data in JSON format, and then perform a two-level loop traversal. In the first level of loop, iterate through each element and wrap its inner elements with {}. In the second level of loop, traverse the attributes of each element, use " to wrap the attribute name and attribute value, and use , to separate different attributes. At the end, use rtrim The function deletes the redundant , and wraps it with ].

We will get this result:

[{"id":"1","name":"小明","age":"18","gender":"男"},{"id":"2","name":"小红","age":"20","gender":"女"},{"id":"3","name":"小刚","age":"19","gender":"男"}]
Copy after login

In short, through From the above example, we can see that it is not difficult to convert a two-dimensional array into JSON format data in PHP. You only need to use the json_encode function. If you need more complex conversion, you can according to the actual Need to choose the appropriate method.

The above is the detailed content of How to convert two-dimensional array to json 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