dpdp42233 2012-02-06 09:42
浏览 50
已采纳

使用php将多个嵌套数组写入文件

What I am trying to do is grab a Business Catalyst-generated CSV of my products. But it always includes a whole lot of stuff I don't need.

I have a script that uses fgetcsv(). The code is

<?php
$file_handle = fopen("ProductExport2.csv", "r");
    while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1000000);
            $tableDisplay =  "<tr><td>" . $line_of_text[0] . "</td><td>" . $line_of_text[1] . "</td><td>" . $line_of_text[2] . "</td><td>" . $line_of_text[4] . "</td><td>" . $line_of_text[6] . "</td><td>" .  $line_of_text[49] . "</td></tr>";
            echo $tableDisplay;
    }
fclose($file_handle);
?>

All this does is display the data I want.

However, what I want to do now is write a new CSV file out of this. Using fwrite() only results in writing just the first entry of the data.

Any ideas?

  • 写回答

2条回答 默认 最新

  • dongpa2000 2012-02-06 10:35
    关注

    Something like this.

    <?php
        $file_handle = fopen("ProductExport2.csv", "r");
        $data = '';
        while (!feof($file_handle) ) {
            $line_of_text = fgetcsv($file_handle, 1000000);
            $data .=  $line_of_text[0] . "," . $line_of_text[1] . "," . $line_of_text[2] . "," . $line_of_text[4] . "," . $line_of_text[6] . "," .  $line_of_text[49] . "
    ";
        }
        fclose($file_handle);
    
        $new_file_handle = fopen("ProductExport2_reduced.csv", "w");
        fwrite($new_file_handle, $data);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料