PHP is a commonly used server-side scripting language. Its main function is for the development of dynamic websites. In PHP, we can use arrays to store data. For data interaction in front-end development, we usually use data in JSON format. Therefore, converting arrays in PHP into JSON format data is a very important skill.
Below, I will introduce how to use PHP functions to convert arrays into JSON format data.
The json_encode() function in PHP can convert a PHP array into JSON format data. Its usage is very simple, just pass the PHP array you want to convert to the function. For example, the following is a simple PHP array:
$data = array( "name" => "张三", "age" => 20, "gender" => "男" );
If we want to convert the above array into JSON format data, we only need to use the json_encode() function:
$json = json_encode($data);
json_encode() function also supports multiple encoding options (options), which can be used to control how the JSON string is generated. The most commonly used option is JSON_UNESCAPED_UNICODE, which is used to ensure that Unicode characters are displayed normally. For example, the following is an example of using JSON_UNESCAPED_UNICODE:
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
There are some rules that need to be followed when converting PHP arrays to JSON format data. For example, the key in the PHP array must be a string, and must be wrapped in double quotes in the JSON format data; the value in the PHP array can be a string, number, Boolean value, null, array or object, while in the JSON format data The value can only be a string, number, Boolean value, null, array or object; if there are multiple elements in the PHP array, each element must be separated by commas.
The following is an example of a PHP array containing multiple elements:
$books = array( array("title"=>"《三体》", "author"=>"刘慈欣", "price"=>39.00), array("title"=>"《围城》", "author"=>"钱钟书", "price"=>32.00), array("title"=>"《活着》", "author"=>"余华", "price"=>24.00) );
We can convert the above PHP array into JSON format data:
$json = json_encode($books, JSON_UNESCAPED_UNICODE);
The conversion results are as follows:
[{"title":"《三体》","author":"刘慈欣","price":39},{"title":"《围城》","author":"钱钟书","price":32},{"title":"《活着》","author":"余华","price":24}]
In this JSON format data, each element in the array is converted into a JSON object, each key in the object is converted into a JSON attribute, and each value is retained in the JSON format data.
Summary
It is a very common requirement to use PHP to convert an array into JSON format data, and we can easily achieve this goal using the json_encode() function. However, during the conversion process, you need to pay attention to the conversion rules and encoding options of the array. As long as we understand these basic knowledge points, we can correctly convert PHP arrays into JSON format data and contribute to data interaction in front-end development.
The above is the detailed content of How to convert array to JSON format data in PHP. For more information, please follow other related articles on the PHP Chinese website!