Home > Article > Backend Development > How to convert array to json format in php
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).
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 ]] )
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;
The output result is:
{"name":"Tom","age":23,"gender":"male"}
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 ({}).
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 ]]] )
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);
The output result is:
Array ( [name] => Tom [age] => 23 [gender] => male )
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
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!