使用PHP写入多个(拆分)CSV文件 [英] Writing to multiple (splitting) CSV files with PHP

查看:151
本文介绍了使用PHP写入多个(拆分)CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个简单的函数将写入数组写入CSV文件,其格式如下:

I'm using a simple function to write write arrays to a CSV-file, which look like this:

function writeToCSV($array) {
    $fp = fopen('programmes.csv', 'a');
    fputcsv($fp, $array);
    fclose($fp);
}

简单的饼图。但是,有没有知道什么行号指针在?因为我想要能够在1000行之后开始写一个新文件。为什么?因为我需要能够以一些内存限制将它们导入数据库,并且用15000行解析一个CSV文件是一个没有。

Simple as a pie. However, is there anyway to know what line-number the pointer is at? Because I want to be able to after 1000 lines to begin writing to a new file. Why? Because I need to be able to import them to a database later with some memory constraints, and to parse a CSV-file with 15000 lines is a no-no.

推荐答案

function writeToCSV($array) {
    $i = 1;
    $j = 1;
    $fp = fopen('programmes' . $j . '.csv', 'a');
    foreach($array as $fields) {
        if ($i % 1000 == 0) {
            fclose($fp);
            $fp = fopen('programmes' . $j . '.csv', 'a');
            $j = $j + 1;
        }
        fputcsv($fp, $fields);
        $i = $i + 1;
    }
    fclose($fp);
}

这篇关于使用PHP写入多个(拆分)CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆