How to write php array to txt file

王林
Release: 2023-05-06 13:14:07
Original
2093 people have browsed it

With the development of the Internet, PHP, as an open source server-side scripting language, is widely used in the field of Web development. In PHP development, it is often necessary to write data in an array to a text file. The following introduces a commonly used method of writing PHP arrays into txt files.

1. Create an array and assign values

First, we need to create an array and add some data to the array. You can simply create an array in the following way:

$data = array(
    array('姓名', '年龄', '性别'),
    array('张三', '18', '男'),
    array('李四', '20', '女'),
    array('王五', '22', '男')
);
Copy after login

In the above code, $data is the array we created. There are four elements in the array, and each element is a small array containing three elements.

2. Save the array data into a txt file

Next, we need to save the data in the above array into a txt file. The steps are as follows:

1. Open the file and write data

You can open the file and write data to the file through PHP's file operation functions fopen() and fwrite(). The sample code using these two functions is as follows:

$fp = fopen('data.txt', 'w');  // 打开文件data.txt,并写入方式为覆盖写入
foreach ($data as $line) {  // 遍历数组数据,每次取出一个小数组
    fwrite($fp, implode("\t", $line)."\n"); // 将将小数组中的元素使用制表符糅合后写入文件,并在行末添加换行符
}
fclose($fp);  // 关闭文件
Copy after login

In the above code, we use the fopen() function to open the file data.txt in overwriting mode, and assign the file handle to the variable $fp. Next, we use foreach() to loop through each element in the array $data. In the loop, we use PHP's implode() function to combine the elements in the small array with tab characters, use the fwrite() function to write it to the file, and add a newline character at the end of each line of data. Finally, we close the file handle through the fclose() function and the write operation is completed.

2. Check whether the file has been written successfully

After the operation is completed, we need to check whether the file has been written successfully. You can check whether the file exists and output the file content through the following code:

if (file_exists('data.txt')) {  // 检查文件是否存在
    echo file_get_contents('data.txt');  // 输出文件内容
} else {
    echo '文件data.txt未创建。';
}
Copy after login

In the above code, we use the file_exists() function to check whether the file exists. If the file exists, we use the file_get_contents() function to output the file contents to the page. If the file does not exist, "File data.txt was not created" is output.

3. Complete code

The following is a complete PHP code example, which can write the array into a txt file at one time and check whether the writing is successful:

<?php
$data = array(
    array('姓名', '年龄', '性别'),
    array('张三', '18', '男'),
    array('李四', '20', '女'),
    array('王五', '22', '男')
);

$fp = fopen('data.txt', 'w');
foreach ($data as $line) {
    fwrite($fp, implode("\t", $line)."\n");
}
fclose($fp);

if (file_exists('data.txt')) {
    echo file_get_contents('data.txt');
} else {
    echo '文件data.txt未创建。';
}
?>
Copy after login

In the above code, we combine the code to create the array, write the file and check whether the file writing is successful, and realize the operation of writing the array to the txt file in one go.

Summary

So far, we have introduced a common method of writing PHP arrays into txt files. Through the introduction of this article, readers can master how to use PHP's file operation function to store array data into a txt file and check whether the file is written successfully. I hope this article can be helpful to PHP developers.

The above is the detailed content of How to write php array to txt file. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!