dreamcat/csv_iterator

csv迭代器

v1.0.0 2019-07-12 11:55 UTC

This package is auto-updated.

Last update: 2024-12-16 14:34:35 UTC


README

介绍

csv文件迭代器,可以遍历读取文件

安装教程

composer require dreamcat/csv_iterator

使用说明

  1. 首先确保存在 csv 文件,test.csv,文件内容如下
     header1,header2,header3,header4
     data11,data12,data13,data14
     data21,data22,,
     data31
     data41,data42,data43,data44,data45
    
  2. 编写如下PHP代码

     <?php
     
     $it = new \DreamCat\CsvIterator\CsvIterator("test.csv");
     foreach ($it as $key => $item) {
         echo json_encode([$key, $item]);
     }
    
  3. 输出如下:
     [0,{"header1":"data11","header2":"data12","header3":"data13","header4":"data14"}]
     [1,{"header1":"data21","header2":"data22","header3":"","header4":""}]
     [2,{"header1":"data31","header2":null,"header3":null,"header4":null}]
     [3,{"header1":"data41","header2":"data42","header3":"data43","header4":"data44","4":"data45"}]
    
  4. 会自动跳过空行,如果文件不存在或打不开,会抛出异常