PHP - 将大型CSV解析成数组数组 - 内存大小耗尽 [英] PHP - parse large CSV into array of arrays - memory size exhausted
问题描述
我想请求帮助这个任务:
我有大的CSV:680列x 1300行,我需要解析按列(我需要有数组
<$ c $ c> $ input =data / input.csv;
if(($ handle = fopen($ input,r))!== false){
$ filesize = filesize($ input);
while(($ data = fgetcsv($ handle,$ filesize,$))!== false){
for($ i = 0; $ i< count($ data); $ i ++){
$ this-> columns [$ i] [] = $ data [$ i];
}
}
fclose($ handle);问题是,我收到此错误
致命错误:允许在第72行的/www/classes/Analyser.php中耗尽134217728字节(尝试分配16384字节)的内存大小
第72行是 $ this-> columns [$ i] [] = $ data [$ i];
不允许我增加内存限制。有没有办法有效地编写代码,所以没有必要这么多的内存,或者只是这么大的CSV的问题?
感谢大家。 p>
解决方案
function analyze_column($ column){
// b $ b return $ the_results;
}
$ a = 0;
$ results = array();
while($ a< COUNT_OF_COLUMNS){
$ column = Array();
while(($ data = fgetcsv($ handle,$ filesize,$))!== false){
array_push($ column,$ data [$ a]);
}
$ results [] = analyze_column($ column);
$ a ++;
}
I would like to ask for help with this task: I have large CSV: 680 columns x 1300 rows and I need to parse it by columns (I need to have array of arrays of column values).
I use this code for this:
$input = "data/input.csv"; if (($handle = fopen($input, "r")) !== false) { $filesize = filesize($input); while (($data = fgetcsv($handle, $filesize, "$")) !== false) { for ($i = 0; $i < count($data); $i++) { $this->columns[$i][] = $data[$i]; } } fclose($handle);
Problem is, that I get this error
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes) in /www/classes/Analyser.php on line 72
line 72 is $this->columns[$i][] = $data[$i];
My hosting does not allow me to increase the memory limit. Is there a way to write the code efficiently, so there is no need for so much memory, or is it just problem of such large CSV?
Thanks everyone.
解决方案function analyze_column($column) { // do your work here return $the_results; } $a = 0; $results = array(); while ($a < COUNT_OF_COLUMNS) { $column = Array(); while (($data = fgetcsv($handle, $filesize, "$")) !== false) { array_push($column,$data[$a]); } $results[] = analyze_column($column); $a++; }
这篇关于PHP - 将大型CSV解析成数组数组 - 内存大小耗尽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!