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', '男') );
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); // 关闭文件
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未创建。'; }
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未创建。'; } ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
