Home  >  Article  >  Backend Development  >  How to convert array to JSON format in PHP

How to convert array to JSON format in PHP

PHPz
PHPzOriginal
2023-04-04 09:11:23441browse

When developing using PHP language, it is often necessary to convert PHP arrays into JSON format. JSON format is very convenient to use in data exchange and transmission, and PHP's own JSON library can easily convert PHP arrays into JSON format data, and also supports converting JSON format data into PHP arrays.

PHP arrays can accommodate a variety of data types, including strings, integers, floating point numbers, Boolean values, NULL and other types. The JSON format can also accommodate these types of data. More importantly, the JSON format data is a lightweight data exchange format for text-based data exchange and has been widely used in Internet applications. .

So, how to convert an array into JSON format data in PHP?

PHP provides a very simple function, json_encode(), which can convert a PHP array into a string in JSON format. The following is a code example of using the json_encode() function to convert a PHP array into JSON format. :

<?php
$arr = array(&#39;name&#39;=>'Jack', 'age'=>30, 'address'=>'New York');
$json = json_encode($arr);
echo $json;
?>

In the above code, $arr is an associative array, which contains three key-value pairs of name, age and address. After encoding it using the json_encode() function, the $json variable will contain the following JSON format string:

{"name":"Jack","age":30,"address":"New York"}

Since JSON is a text-based format, unlike PHP arrays, in JSON All strings must be enclosed in double quotes, which is a fixed requirement of the JSON format. At the same time, the key-value pairs of the associative array are also converted into attributes in JSON format. Therefore, you need to pay attention to these details when encoding using the json_encode() function.

Of course, if you need to convert JSON format data into a PHP array, you can use the json_decode() function. This function can decode the JSON format string into a PHP array. The following is a code example:

<?php
$json = &#39;{"name":"Jack","age":30,"address":"New York"}&#39;;
$arr = json_decode($json, true);
print_r($arr);
?>

In the above code, the $json variable contains data in JSON format, and $arr is the PHP array obtained after decoding the JSON data. When calling the json_decode() function, set the second parameter to true, which means converting JSON data into an associative array, otherwise it defaults to converting into a PHP object.

In summary, it is very simple to use PHP to convert an array into JSON format data. You only need to call the json_encode() function. When you need to convert JSON format data into a PHP array, you can use the json_decode() function. These two functions are built-in functions in the core library and are widely used in PHP language development.

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!

Statement:
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