fputcsv不适用于大型阵列 [英] fputcsv does not work with large arrays

查看:90
本文介绍了fputcsv不适用于大型阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用fputcsv(显然)在应用程序中创建表的csv文件.我只是将二维数据数组发布到:

I'm using fputcsv to (obviously) create a csv file of a table in an application. I simply post a 2d array of data to:

function formatoutput(&$vals, $key){
    foreach($vals AS $row=>&$cell){
        $cell = str_replace('"', '""', $cell); 
        $cell = str_replace(array("\n",','), '', $cell); 
    }
}

function outputCSV($array) {
    $outstream = fopen("php://output", "w");
    function __outputCSV(&$vals, $key, $filehandler) {
        fputcsv($filehandler, $vals);
    }
    array_walk($array, "formatoutput");
    array_walk($array, "__outputCSV", $outstream);
    fclose($outstream);
}

outputCSV(unserialize($_POST['data']));

因此,对于数组中的每一行,它将其添加到csv文件中.好吧,这很好,直到我传递了大约500行或更多行的数组.

so, for each line in the array, it appends it to the csv file. Well, this works fine until I pass in an array with around 500 or more lines.

使用此功能时是否有尺寸限制?该文档没有提及任何内容.还是有一个PHP设置会限制大小?

Is there a size limit when using this function? The documentation doesn't mention anything. Or is there a PHP setting that would limit the size?

此处存在此问题:基本上问同样的事情,但是即使答案很明确,也没有真正解释任何可能的解决方案.它只是指出:

Which basically asks the same thing, but even though there is a marked answer, it doesn't really explain any possible solutions. It simply states:

听起来像是您的环境设置有问题-与代码或技术限制无关."

"Sounds like a problem with your environment settings - not with the code or a limitation of the technologies."

推荐答案

PHP将此类变量加载到内存中.根据您的php.ini设置,每个脚本都有允许使用的最大内存量.

PHP loads variables like these into memory. And every script has a maximum amount of memory it is allowed to use, based on your php.ini settings.

此外,复制变量时,它们可能占用内存的两倍.源内存和目标内存.我不确定fputcsv的内部结构,但有可能将整个数组在内部转换为csv对象",然后写出到文件中.

Also, when variables are copied, they can take up double the space in memory. The source memory, and the destination memory. I'm not sure the internals of fputcsv, but it's possible that the entire array is being converted to a csv "object" internally, then written out to file.

因此,请记住,每次复制时,您的内存使用量都可能翻倍.

So, remember that you're possibly doubling your memory usage any time you copy.

至少,请在php.ini中检查"memory_limit".如果您需要特别针对一个脚本增加它,则可以使用以下命令在您的脚本中进行设置:

In the very least, check your "memory_limit" in your php.ini. If you need to increase it for one script in particular, then you can set it in your script with:

ini_set('memory_limit','16M');

这篇关于fputcsv不适用于大型阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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