With the development of the Internet, data interaction becomes more and more frequent, among which JSON (JavaScript Object Notation) has become one of everyone’s favorite data transmission formats. Therefore, processing JSON data is becoming increasingly important. This article will introduce the use of PHP to process the addition, deletion, modification, and search operations of JSON files.
Before formally explaining the operation of JSON by PHP, first understand the grammatical basis of JSON. JSON uses key-value pairs to record data, where the key is enclosed in double quotes, followed by a colon, and then a value (can be any type of value, such as: string, number, Boolean value, array, object , null). Multiple key-value pairs are separated by commas, and the entire JSON is surrounded by braces {}.
For example:
{
"name": "张三", "age": 25, "city": "上海", "friends": [ "李四", "王五", "赵六" ]
}
PHP built-in There are some functions for processing JSON data, including:
json_encode(): used to convert PHP arrays into JSON format strings.
json_decode(): used to convert JSON format string to PHP array/object.
json_last_error(): used to obtain the error information generated by the last JSON encoding/decoding.
The following will introduce the specific use of these functions through examples.
We assume that there is a JSON file data.json with the following content:
{
"name": "张三", "age": 25, "city": "上海", "friends": [ "李四", "王五", "赵六" ]
}
We need to read this JSON file and parse it into a PHP array, just use the json_decode() function, the code is as follows:
// 读取JSON文件 $json_str = file_get_contents('data.json'); // 解析JSON文件为PHP数组 $data = json_decode($json_str, true); // 打印数组 print_r($data);
?>
The output result is as follows:
Array
(
[name] => 张三 [age] => 25 [city] => 上海 [friends] => Array ( [0] => 李四 [1] => 王五 [2] => 赵六 )
)
Among them, the second parameter of the json_decode() function is true, indicating decoding into a PHP array. If this parameter is not passed or false is passed, it will be decoded into a PHP object.
In PHP, you can use the json_encode() function to convert a PHP array into a string in JSON format. For example, to convert the array $data in the above example into a string in JSON format, the code is as follows:
// 将PHP数组转换为JSON格式字符串 $json_str = json_encode($data, JSON_UNESCAPED_UNICODE); // 打印JSON格式字符串 echo $json_str;
?>
The output result is as follows :
{"name":"Zhang San","age":25,"city":"Shanghai","friends":["李四","王五","Zhao Liu" ]}
As you can see, the second parameter JSON_UNESCAPED_UNICODE of the json_encode() function means not to escape Chinese into Unicode encoding.
Next, we need to write the above JSON format string into a file. You can use the file_put_contents() function. The code is as follows:
// 将JSON格式字符串写入文件 file_put_contents('result.json', $json_str);
?>
The first parameter is the saved file name, and the second parameter is the JSON format string to be written.
Modifying the JSON file actually means modifying its corresponding PHP array, and then converting the array into a JSON format string, overwriting the original Just file. Suppose we want to change Zhang San’s age to 30 years old. The code is as follows:
// 修改PHP数组 $data['age'] = 30; // 将PHP数组转换为JSON格式字符串 $json_str = json_encode($data, JSON_UNESCAPED_UNICODE); // 覆盖原来的JSON文件 file_put_contents('data.json', $json_str);
?>
To delete JSON files, you can use the unlink() function, as follows:
// 删除JSON文件 unlink('data.json');
?>
When processing data in JSON, we often need to query the value of a certain key. If you need to query Zhang San's city, the code is as follows:
// 读取JSON文件 $json_str = file_get_contents('data.json'); // 解析JSON文件为PHP数组 $data = json_decode($json_str, true); // 查询张三的城市 $city = $data['city']; // 打印张三的城市 echo $city; // 上海
?>
As you can see from this example, JSON data is processed in PHP There is no difference from dealing with ordinary arrays or objects.
Summary
This article introduces the addition, deletion, modification and query operations of PHP on JSON files, including reading and parsing, writing and generating, modifying, deleting and querying. Of course, these operations are based on JSON files or strings, not databases. Therefore, when we need to process large amounts of data, it is recommended to use a database instead of JSON files.
The above is the detailed content of PHP adds, deletes and modifies json files. For more information, please follow other related articles on the PHP Chinese website!