Home >Backend Development >PHP Problem >How to store array data in php
In the development process using PHP, arrays are one of the commonly used data structures. In some cases, we need to save an array data to a file, database or other storage medium for subsequent use. So, how to store array data in PHP? This article will introduce in detail several common PHP array data storage methods.
serialize() function can serialize a variable (including an array) into a string, which can be saved to a file, etc. in the storage medium. This string can be deserialized into the original variable (including array) through the unserialize() function.
The following are examples of using the serialize() and unserialize() functions:
// 定义一个数组 $array = array( 'name' => '张三', 'age' => 18, 'gender' => '男' ); // 序列化数组 $data = serialize($array); // 将序列化后的数据保存到文件 file_put_contents('data.txt', $data); // 读取保存的数据并反序列化 $data = file_get_contents('data.txt'); $array = unserialize($data); // 输出反序列化后的数组 print_r($array);
In the above code, we use the serialize() function to first serialize the $array array into a string, and then pass file_put_contents () function saves the serialized string to a file. Next, we use the file_get_contents() function to read the saved data and use the unserialize() function to deserialize it into the original array. Finally, the deserialized array is output through the print_r() function.
It should be noted that when the serialize() function serializes an array, it will save the key name, key value, array length and other information together into a string. Therefore, during deserialization, the exact same array structure needs to be used to restore the complete array data. If the serialized array contains objects or resources, problems may occur during deserialization.
json_encode() function can convert a variable (including an array) into a string in JSON format, and this string can also be saved into the file. This string can be parsed into the original variables (including arrays) through the json_decode() function.
The following are examples of using the json_encode() and json_decode() functions:
// 定义一个数组 $array = array( 'name' => '张三', 'age' => 18, 'gender' => '男' ); // 将数组转换成 JSON 格式字符串 $data = json_encode($array); // 将 JSON 格式字符串保存到文件 file_put_contents('data.txt', $data); // 读取保存的数据并解析成原来的数组 $data = file_get_contents('data.txt'); $array = json_decode($data, true); // 输出解析后的数组 print_r($array);
In the above code, we use the json_encode() function to convert the $array array into a JSON format string, and then Save it to a file via the file_put_contents() function. Next, we use the file_get_contents() function to read the saved data and use the json_decode() function to parse it into the original array. It should be noted that when using the json_decode() function, you need to set the second parameter of the json_decode() function to true, otherwise an object will be parsed instead of an array.
It is important to note that the json_encode() function can only convert data types such as numeric values, strings, Boolean values, and null into JSON format strings. Therefore, if the array contains data types such as objects or resources, using the json_encode() function will throw an error.
The var_export() function can export a variable (including an array) into a string form of PHP executable code. The exported string Contains information such as the key name and key value of the array. This string can also be saved to a storage medium such as a file. This string can be imported back into the PHP script via the eval() function and converted into the original variable (including an array).
The following is an example of the use of the var_export() function and the eval() function:
// 定义一个数组 $array = array( 'name' => '张三', 'age' => 18, 'gender' => '男' ); // 将数组导出成 PHP 执行代码的字符串形式 $data = var_export($array, true); // 将导出的字符串保存到文件 file_put_contents('data.php', '<?php return ' . $data . ';'); // 将保存的数据导入回 PHP 中,并转换成原来的数组 $array = include('data.php'); // 输出导入后的数组 print_r($array);
In the above code, we use the var_export() function to export the $array array into characters of PHP executable code string form and then save it to a file through the file_put_contents() function. Then, we added the two strings '' at the beginning and end of the saved data, so that this PHP file can be directly imported back by the include() function PHP script and return the corresponding variables (including arrays). Finally, we use the print_r() function to output the imported array.
It should be noted that when the var_export() function exports an array, the key name and key value of the array will be saved together in the exported string. Therefore, when importing, you need to use the exact same array structure to restore the complete array data. If the exported array contains objects or resources, problems may occur during import.
Summary
This article introduces several common PHP array data storage methods, namely serialize() and unserialize() functions, json_encode() and json_decode() functions, and var_export() function and the eval() function. For different usage scenarios, we can choose the appropriate method to store array data in files, databases or other storage media for subsequent use.
The above is the detailed content of How to store array data in php. For more information, please follow other related articles on the PHP Chinese website!