【问题标题】:PHP convert CSV files into arrays and intersectPHP将CSV文件转换为数组并相交
【发布时间】:2015-09-11 16:59:47
【问题描述】:

我有六个 CSV 文件,我想将它们转换为单独的数组,然后将这些数组相交以查看每个数组中相似的所有值。到目前为止我有这个:

 $directory = "/csv/CSS_AUDIT/";

 if (! is_dir($directory)) {
     exit('Invalid directory path');
 }

 $files = array();

 foreach (scandir($directory) as $file) {
     if ('.' === $file) continue;
     if ('..' === $file) continue;

     $files[] = $file;

      $row = 1;

     if (($handle = fopen("/csv/CSS_AUDIT/$file", "r")) !== FALSE) {
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
             $num = count($data);
             $row++;
             for ($r=0; $r < $num; $r++) {
                 echo $data[$r];
                 if (!empty($data[$r])) 
                    echo "\n";
             }
         }
         fclose($handle);
     }

 }

现在它正在遍历文件并将所有内容都放在一个大数组中,我只是有点卡住了。关于如何实现我正在尝试做的任何建议?

【问题讨论】:

    标签: php arrays csv intersect


    【解决方案1】:

    如果文件不是太大,您可以将它们加载到数组中,并在添加之前检查数组中是否不存在此类值,或者在完成后运行array_unique()。参考this

    如果文件太大,您将不得不使用其他一些更复杂且耗时的方法(例如每次插入新行之前读取(希望排序的)输出)。

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2012-08-25
      • 2020-11-05
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2016-05-21
      相关资源
      最近更新 更多