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

How to convert array to json format in php

PHPz
Release: 2023-04-25 09:46:17
Original
819 people have browsed it

With the continuous development of Web technology, front-end and back-end data interaction has become more and more frequent. In this process, JSON format has become a very popular data exchange format. As a dynamic language, PHP also has a very rich library and functions for processing JSON format data. Here, we mainly introduce how PHP converts an array into JSON format.

1. What is JSON format

JSON (JavaScript Object Notation) is a lightweight data exchange format, usually used for front-end and back-end data transmission. It adopts a syntax format similar to JavaScript objects and has good support in various programming languages, such as JavaScript, PHP, Python, Java, C#, Ruby, etc. JSON format data is a string encoded in JSON format.

2. How to convert arrays to JSON format in PHP

There are two functions in PHP about JSON format: json_encode and json_decode. These two functions are used to convert PHP data types into JSON format strings (encoding), and convert JSON format strings into PHP data types (decoding).

  1. Convert array to JSON format

In PHP, you can use the json_encode function to convert an array into a JSON format string. The syntax of this function is:

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
Copy after login

Among them, the $value parameter is the value to be converted, which can be a string, number, Boolean value, array or object. The $options parameter is an optional parameter used to specify encoding options. The $depth parameter is an optional recursion depth (default is 512), used to limit the depth of recursion to prevent exceeding the depth of the PHP recursion stack.

The following is a simple example that demonstrates how to convert a PHP array into a JSON format string:

$person = array(
    'name' => 'Tom',
    'age' => 23,
    'gender' => 'male'
);

$json = json_encode($person);

echo $json;
Copy after login

The output result is:

{"name":"Tom","age":23,"gender":"male"}
Copy after login

As you can see, the json_encode function Convert the $person array to a JSON format string. In JSON format, each attribute is a string surrounded by double quotes (""), and the attribute name and attribute value are separated by colons (:). Different attributes are separated by commas (,), and the entire JSON format data is surrounded by a pair of curly braces ({}).

  1. JSON format to array

In PHP, the JSON format string can be converted into a PHP array or object through the json_decode function. The syntax of this function is:

mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )
Copy after login

Among them, the $json parameter is the JSON format string to be decoded. The $assoc parameter is an optional parameter. If set to TRUE, the returned result is an associative array; otherwise, the returned result is an object. The $depth parameter and $options parameter are the same as the json_encode function.

The following is a simple example that demonstrates how to convert a JSON format string into a PHP array:

$json = '{"name":"Tom","age":23,"gender":"male"}';
    
$person = json_decode($json, true);
    
print_r($person);
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 23
    [gender] => male
)
Copy after login

As you can see, the json_decode function Convert a JSON format string into a PHP array. In this array, the attribute name becomes the array key, and the attribute value becomes the array value.

3. Notes

  1. The values ​​in the array must be data types that support JSON format. Unsupported data types need to be type converted first.
  2. When using the json_encode function, be sure to pay attention to the encoding format to avoid Chinese garbled characters.
  3. For the JSON format string to be finally output, you can set the JSON_UNESCAPED_UNICODE option through the second parameter $options of the json_encode function to ensure that the output result does not escape Unicode characters.
  4. For the json_decode function, you need to pay attention to whether the input JSON format string conforms to the JSON specification, otherwise the decoding may fail.

To sum up, converting an array to JSON format in PHP is very simple. You only need to use two small and powerful functions. Using PHP to process JSON format data can make front-end and back-end data interaction simpler and more effective.

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