I'm using php(fgetcsv) in order to parse a csv file. The file is continuously updated and quite large. Is it possible to only return a selected number of rows(say the first 10 rows for example)?
Asked
Active
Viewed 682 times
2 Answers
0
I think you can read data row wise and return whichever rows you want by keeping a track at current row number.
$pickedRows = array(1, 3, 5);
$currentRow=0;
$theData = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(in_array(currentRow, $pickedRows))
$theData[] = $row;
$currentRow++;
}
fclose($handle);
}

abhinsit
- 3,214
- 4
- 21
- 26
0
A simple while loop with your limit parameter $rowlimit
will suffice
$rowlimit=10;
while ( ($fileinfo = fgetcsv($csvfil)) && ($rowlimit >= 0) )
{
//Your Statements
$rowlimit--;
}

Shankar Narayana Damodaran
- 68,075
- 43
- 96
- 126