从csv返回选定的行

时间:2013-08-16 09:25:23

标签: php fgetcsv

我正在使用php(fgetcsv)来解析csv文件。该文件不断更新,非常大。是否可以仅返回选定数量的行(例如前10行)?

2 个答案:

答案 0 :(得分:0)

我认为您可以按行方式读取数据,并通过将轨道保持在当前行号来返回您想要的任何行。

$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);
}

答案 1 :(得分:0)

使用限制参数$rowlimit的简单while循环就足够了

$rowlimit=10;
while ( ($fileinfo = fgetcsv($csvfil)) && ($rowlimit >= 0) )
{
//Your Statements
$rowlimit--;
}