PHP - parse large CSV into array of arrays - memor

2019-09-19 16:57发布

问题:

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.

回答1:

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++;
}