Home  >  Article  >  Backend Development  >  How to store php array in txt

How to store php array in txt

PHPz
PHPzOriginal
2023-04-19 09:17:47769browse

PHP array is a very powerful and flexible data type capable of storing and manipulating large amounts of data. Sometimes, we need to save array data to a text file (.txt) so that it can be exported and transferred, or loaded and used again later. In this article, we will explain how to save an array to a .txt file in PHP.

First, we need to convert the array data into string format. There are some functions provided in PHP to achieve this. Among them, the serialize() function converts the array into string format for easy storage. We can use the following code to demonstrate:

$arr ​​= array("name" => "John", "age" => "30", "city" => "New York") ;
$serialized_data = serialize($arr);
echo $serialized_data;

The above code will output the following string:

a:3:{s:4:" name";s:4:"John";s:3:"age";s:2:"30";s:4:"city";s:8:"New York";}

Now we have successfully converted the array data into string format. Next, we need to write the string data to a text file. In PHP, you can open a .txt file by using the fopen() function and then write data to the file.

The following is a complete code example for saving array data to a .txt file:

// Declare the array
$arr ​​= array("name" => "John", " age" => "30", "city" => "New York");

// Serialize array data into string format data
$serialized_data = serialize($arr );

//Open the .txt file
$file = fopen("data.txt","w");

//Write the data to the .txt file
fwrite($file, $serialized_data);

// Close the .txt file
fclose($file);

The above code will create a file named data in the current directory. txt file and save the array data in it. Please note that here we use the "w" parameter to overwrite the file, if the file already exists, it will be overwritten.

Now, we have successfully saved the array data to the .txt file. If you need to read the file and reload the array, you can use the following code:

// Open the .txt file
$file = fopen("data.txt","r");

// Read the data in the .txt file
$data = fread($file, filesize("data.txt"));

// Close the .txt file
fclose($file);

//Deserialize data into an array
$arr ​​= unserialize($data);

//Print array
print_r($arr );

The above code reads data from a file and then deserializes it into an array using the unserialize() function. Finally, we can print the array to confirm that the array data was successfully loaded from the .txt file into the PHP array.

Summary:

In PHP, saving array data to a .txt file is very simple. First use the serialize() function to convert the array data into string format, then use the fopen() function to open the .txt file and write the string data into the file. If you need to reload the array data, you can use the fopen() function to open the .txt file, read the string data in it, and use the unserialize() function to deserialize it into an array.

The above is the detailed content of How to store php array in txt. 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