繁体   English   中英

尝试使用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. 获取行,并创建一个新的文本文件,其名称是行数组中的第一个元素(即该行的csv文件中的第一列)
  4. 将csv文件中的第二列放入新的文本文件中
  5. 关闭文件

那有意义吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM