Transform.php
public static function csvFileToArrayGenerator(string $filename = '', string $delimiter = ',')
    {
        $complete = false;
        try {
            $header = null;
            if (($handle = fopen($filename, 'r')) !== false) {
                while (($row = fgetcsv($handle, 4096, $delimiter)) !== false) {
                    if (!$header) {
                        $header = $row;
                    } else {
                        yield array_combine($header, $row);
                    }
                }
            }
            $complete = true;
        } finally {
            if (!$complete) {
                // cleanup when loop breaks
            } else {
                // cleanup when loop completes
            }
        }
        // Do something only after loop completes
    }
public static function csvFileToArrayGenerator(string $filename = '', string $delimiter = ',')
    {
        $complete = false;
        try {
            $header = null;
            if (($handle = fopen($filename, 'r')) !== false) {
                while (($row = fgetcsv($handle, 4096, $delimiter)) !== false) {
                    if (!$header) {
                        $header = $row;
                    } else {
                        yield array_combine($header, $row);
                    }
                }
            }
            $complete = true;
        } finally {
            if (!$complete) {
                // cleanup when loop breaks
            } else {
                // cleanup when loop completes
            }
        }
        // Do something only after loop completes
    }
public static function csvFileToArrayGenerator ( string $ filename = '' , string $ delimiter = ',' ) { $ complete = false ; try { $ header = null ; if ( ( $ handle = fopen ( $ filename , 'r' ) ) !== false ) { while ( ( $ row = fgetcsv ( $ handle , 4096 , $ delimiter ) ) !== false ) { if ( ! $ header ) { $ header = $ row ; } else { yield array_combine ( $ header , $ row ) ; } } } $ complete = true ; } finally { if ( ! $ complete ) { // cleanup when loop breaks } else { // cleanup when loop completes } } // Do something only after loop completes }
Read a csv using a generator in order to avoid memory issues
@param string $filename
@param string $delimiter
@return \Generator
Read a csv using a generator in order to avoid memory issues