In PHP programming, array is a commonly used data type used to store a series of related data. Sometimes we need to save the data in the array to a file or database for next time use. This article will introduce how to save PHP arrays completely.
1. Use a file to save an array
Before using a file to save an array, we need to create an empty file first. This task can be accomplished using the file_put_contents() function:
$file = 'array.txt'; file_put_contents($file, '');
Next, we can use the serialize() function to serialize the array and write it to a file:
$array = array('apple', 'orange', 'banana'); $data = serialize($array); file_put_contents($file, $data);
If needed re-read For the saved array, you can use the file_get_contents() function to read the data in the file, and use the unserialize() function to deserialize it into an array:
$data = file_get_contents($file); $array = unserialize($data); print_r($array);
Using this method to save the array can ensure the integrity of the array, Includes keys and values. However, it should be noted that the data stored in this file is not readable and is only suitable for data transfer and storage between PHP programs.
2. Use JSON format to save arrays
JSON format is a lightweight data exchange format that is easy to read and write. PHP provides json_encode() and json_decode() functions to easily convert PHP arrays to JSON format and back:
$array = array('apple', 'orange', 'banana'); $json = json_encode($array); file_put_contents($file, $json);
If you need to read the saved JSON format data and convert it to a PHP array, you can use file_get_contents( ) function reads the data and uses the json_decode() function to convert it into an array:
$json = file_get_contents($file); $array = json_decode($json, true); print_r($array);
Compared with serialization, saving arrays in JSON format can make the data easier to read and parse, and more versatile. However, it should be noted that the JSON format does not support binary data as well as serialization, so it is not suitable for storing binary data such as images.
3. Use the database to save the array
Save the array to the database, so you can easily perform operations such as additions, deletions, modifications, and searches. We can store each element in the array in a field in the database, or we can store the entire array in a field using serialization or JSON format.
Taking the MySQL database as an example, you first need to connect to the database. The connection can be achieved using the mysqli_connect() function:
$host = 'localhost'; $user = 'root'; $password = '123456'; $dbname = 'mydatabase'; $mysqli = mysqli_connect($host, $user, $password, $dbname); if (!$mysqli) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); }
Next, we can define an array and store it in the database:
$array = array('apple', 'orange', 'banana'); $data = serialize($array); $sql = "INSERT INTO mytable (array_field) VALUES ('$data')"; if ($mysqli->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $mysqli->error; }
If you need to read the saved array, you can use SELECT The statement queries the database and then deserializes the results into an array:
$sql = "SELECT array_field FROM mytable WHERE id=1"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $data = $row['array_field']; $array = unserialize($data); print_r($array); } } else { echo "0 results"; }
Using this method to save the array combines the integrity and readability of the array. However, it should be noted that storing large arrays may occupy a large amount of storage space, so you need to choose an appropriate storage method based on the actual situation.
Summary
This article introduces three common methods to save PHP arrays, including serialization, JSON format and database. An appropriate storage method needs to be selected based on the actual situation to ensure data integrity and readability.
The above is the detailed content of How to save a php array completely. For more information, please follow other related articles on the PHP Chinese website!