Home > Backend Development > PHP Problem > How to convert array into json format in php

How to convert array into json format in php

PHPz
Release: 2023-04-20 15:37:00
Original
496 people have browsed it

In modern programming, cross-platform and data interaction have become the norm. This requires us to use a common format when transferring data between different platforms and systems. XML and JSON are currently popular formats, and JSON is popular because of its concise language and easy to read and parse. This article will introduce how to convert PHP array to JSON.

The first thing you need to understand is that PHP provides us with some functions to convert JSON. We can use the json_encode() function to convert the array into a string in JSON format to facilitate the transmission and exchange of data between different systems. The json_encode() function encodes a PHP array into a string in JSON format and returns the encoded JSON string. Its use is very simple, just pass in the array that needs to be encoded.

The following is a sample code:

$data = array(
    'name' => 'Michael',
    'age' => 29,
    'gender' => 'male'
);

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

The above code converts the array $data into a string in JSON format, and the output result is:

{"name":"Michael","age":29,"gender":"male"}
Copy after login

You can see that the output In addition to adding quotation marks between data items and using colons instead of equal signs, the JSON format string also adds corresponding symbols at the beginning and end of the braces, so that the JSON string is the standard JSON format.

In addition to converting arrays into JSON strings, you may also need to convert JSON strings into arrays for use in PHP code. At this time, we can use the json_decode() function to convert the JSON string into a PHP array. The usage method is as follows:

$json = '{"name":"Michael","age":29,"gender":"male"}';

$data = json_decode($json, true);
print_r($data);
Copy after login

The above code parses the JSON format string into a PHP array and uses the print_r() function Output the array result, the result is as follows:

Array
(
    [name] => Michael
    [age] => 29
    [gender] => male
)
Copy after login

As you can see, the json_decode() function parses the JSON string into a PHP array and returns it; of course, if you do not need to convert it into a PHP array, you do not need to pass it in. The second parameter directly outputs the parsed object.

At this point, we have learned how to convert between PHP arrays and JSON format strings. The array type in the PHP language is a very flexible data type. If data needs to be exchanged or transmitted between different systems, we only need to call json_encode() to convert the PHP array into a JSON format string, or call json_decode() This can be achieved by converting the JSON format string into a PHP array.

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