尝试使用 php 将每个数组保存到 txt 文件


Trying to save each array to a txt file using php

<?php
$file = fopen("test2.csv","r");
while(! feof($file)) {
    print_r(fgetcsv($file));
    $textfilename=[0].".txt";
    file_put_contents ([1]);
}
fclose($file);
?> 

这将需要这样的数据

Array ( 
    [0] => name 
    [1] => download information 
) 
Array ( 
    [0] => Sense and Sensibility Instant Digital Download 
    [1] => Please visit the following link to download your digital product: http://archive.org/download/0_sense_and_sensibility_librivox/Sense_Sensibility_1107_64kb_mp3.zip 
) 

对于每个 0 =>将其作为文件名。然后将 [1] 存储到文件中并保存。但是我遇到了一个错误。

你的意思是这样?尚未测试。

<?php
$file = fopen("test2.csv","r");
while(($line = fgetcsv($handle, 1000, ",")) !== FALSE)
  {
  $textfilename=$line[0].".txt";
  file_put_contents($textfilename, $line[1]);
  }
fclose($file);
?> 
if (($handle = fopen("test2.csv", "r")) !== FALSE) 
{
    while (($line = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        $textfile = $line[0] . ".txt";
        file_put_contents($textfile, $line[1]);
    }
    fclose($handle);
}

此代码;

  1. 确保您可以打开和读取csv文件
  2. 对于 csv 文件中的每一行
  3. 获取该行,
  4. 并创建一个新的文本文件,其名称是线条数组中的第一个元素(即该行的 csv 文件中的第一列)
  5. 将 csv 文件中的第二列放入新文本文件中
  6. 关闭文件

这有意义吗?