I would like to introduce about usege of a Generator in PHP. For that purpose i've taken this as an example to learn.
Suppose you've a 4GB of CSV file and you need to iterate as a stream resource. Generally our VPS allows 1GB to 2GB of memory. Hence there's no way to get out but have to take help of PHP generator.
Please check the example below as a reference.
function getCsvFileRow($bigDataFile) {
$handler = fopen($bigDataFile, 'r');
if($handler === false){ throw new Exception(); }
while(feof($handler) === false){ yield fgetcsv($handler); }
fclose($handler);
}
foreach(getCsvFileRow('pathToCsvFile/dataFile.csv') as $dataRow) {
print_r($dataRow);
}
* Further details please refer Standard PHP Library (SPL) iterators.
No comments:
Post a Comment