How to convert array to json format in php

PHPz
Release: 2023-04-26 09:33:06
Original
673 people have browsed it

In Web development, JSON (JavaScript Object Notation) is a popular data format. It is a lightweight data exchange format and is widely used for data transmission between servers and clients. As a mainstream web development language, PHP provides a simple way to convert an array into JSON format.

In PHP, there is a built-in function json_encode(), which can convert a PHP array into a JSON string and return it. Here is an example:

$myArray = array(
    "name" => "John",
    "age" => 30,
    "city" => "New York"
);

$myJSON = json_encode($myArray);
echo $myJSON;
Copy after login

The output is:

{"name":"John","age":30,"city":"New York"}
Copy after login

json_encode()The function accepts a PHP array as a parameter and returns a JSON string. In the above example, we defined a PHP array containing three key-value pairs. Then we call the json_encode() function to convert the array into a JSON string and assign the result to the variable $myJSON. Finally, we use the echo statement to output this JSON string.

In addition to ordinary arrays, the json_encode() function also supports associative arrays, nested arrays and objects. The following is an example of a nested array:

$myArray = array(
    "name" => "John",
    "age" => 30,
    "address" => array(
        "street" => "123 Main St",
        "city" => "New York",
        "state" => "NY"
    )
);

$myJSON = json_encode($myArray);
echo $myJSON;
Copy after login

The output is:

{
    "name": "John",
    "age": 30,
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "state": "NY"
    }
}
Copy after login

In the above example, we defined a PHP array that contains a nested array. Among them, the value corresponding to the address key is another array, which contains address information such as street, city, and state. Then, we use the json_encode() function again to convert the array into a JSON string and output it.

In addition to the json_encode() function, PHP also provides a built-in function json_decode(), which can convert a JSON string into a PHP object or array. The following is an example:

$myJSON = '{"name":"John","age":30,"city":"New York"}';
$myArray = json_decode($myJSON, true);
print_r($myArray);
Copy after login

The output result is:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)
Copy after login

json_decode()The function accepts a JSON string as a parameter and returns the corresponding PHP object or array. In the above example, we define a JSON string and assign it to the variable $myJSON. Then, we use the json_decode() function to convert the JSON string into a PHP array and assign the result to the variable $myArray. Finally, we use the print_r() function to output the array.

To summarize, PHP provides built-in functions to easily convert a PHP array into a JSON string. In daily development, we can use the json_encode() function to generate a JSON string that meets the requirements. At the same time, PHP also supports converting a JSON string into a PHP object or array, or transmitting them between different systems.

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