我有一个包含100000客户详细信息的大csv文件。我想为我的android应用程序获取数据。我想从csv文件中读取特定的行。就像我想要客户的所有细节,cust_id
是070507
使用这种方法在php中读取这么大的文件需要花费太多的时间。
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
}
fclose($handle)
}
我想要一个简单的解决方案。如果这是最简单的方法,那么请告诉我如何读取特定的行?
发布于 2015-01-12 23:25:33
可以使用file()将文件放入数组中,然后可以针对特定行并将其解析为CSV。
$lines = file('test.csv');
$row = $lines[70507]; // Assuming one header row
$csvdata = str_getcsv($row); // Parse line to CSV
$num = count($data); // Get number of columns
for ($c=0; $c < $num; $c++) { // Loop through columns
echo $data[$c] . "<br />\n"; // Echo column
}
发布于 2015-01-12 23:20:05
这就是读取特定行的方式。
$ch = fopen($link_to_file);
$found = '';
/* If your csv file's first row contains Column Description you can use this to remove the first row in the while */
$header_row = fgetcsv($read_file);
/* This will loop through all the rows until it reaches the end */
while(($row = fgetcsv($ch)) !== FALSE) {
/* $row is an array of columns from that row starting at 0 */
$first_column = $row[0];
/* Here you can do your search */
/* If found $found = $row[1]; */
/* Now $found will contain the 2nd column value (if found) */
}
https://stackoverflow.com/questions/27916679
复制相似问题