简体   繁体   English

如何遍历php中的多个csv文件

[英]how to traverse through multiple csv files in php

I have a form,i want to take value from form and check this value to the value in csv file. 我有一个表单,我想从表单中获取值并将此值检查为csv文件中的值。 But it can be in any of the multiple csv files. 但是它可以在多个csv文件中的任何一个中。

So how can i traverse through multiple csv files 那么我如何遍历多个csv文件

For example, I have 50 csv files i want to traverse through these 50 csv files until i get the appropriate result. 例如,我有50个csv文件要遍历这50个csv文件,直到获得合适的结果。

I know how to traverse through 1 csv file code i used for this is written below 我知道如何遍历我为此使用的1个CSV文件代码

<?PHP
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
    $line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'test.csv';
$csv = readCSV($csvFile);
echo '<pre>';
print_r($csv);
echo '</pre>';
?>

Like this how can i traverse through multiple csv files. 这样,我如何遍历多个csv文件。

Here is an example: 这是一个例子:

$files = glob('/path/to/dir/*.csv');
foreach($files as $f) {
  $csv = readCSV($f);
  echo '<pre>';
  print_r($csv);
  echo '</pre>';
}

Reference: glob() - function to list files in specific directory with specific pattern 参考: glob() -用于以特定模式列出特定目录中的文件的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM