php遍历CSV类实例

yipeiwu_com5年前PHP代码库

本文实例讲述了php遍历CSV类。分享给大家供大家参考。具体如下:

<?php
class CSVIterator implements Iterator
{ 
  const ROW_SIZE = 4096;
  private $filePointer;
  private $currentElement;
  private $rowCounter;
  private $delimiter;
  public function __construct( $file, $delimiter = ',' )
  {
    $this->filePointer = fopen( $file, 'r' );
    $this->delimiter  = $delimiter;
  }
  public function rewind()
  {
    $this->rowCounter = 0;
    rewind( $this->filePointer );
  }
  public function current()
  {
    $this->currentElement = fgetcsv($this->filePointer,self::ROW_SIZE,$this->delimiter);
    $this->rowCounter++;
    return $this->currentElement;
  }
  public function key()
  {
    return $this->rowCounter;
  }
  public function next()
  {
    return !feof( $this->filePointer );
  }
  public function valid()
  {
    if( !$this->next() )
    {
      fclose( $this->filePointer );
      return FALSE;
    }
    return TRUE;
  }
} // end class
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

解析PHP中的内存管理,PHP动态分配和释放内存

解析PHP中的内存管理,PHP动态分配和释放内存

摘要 内存管理对于长期运行的程序,例如服务器守护程序,是相当重要的影响;因此,理解PHP是如何分配与释放内存的对于创建这类程序极为重要。本文将重点探讨PHP的内存管理问题。 一、 内存在...

PHP小技巧之函数重载

1.可以使用func_get_args()和func_num_args()这两个函数实现函数的重载!! PHP代码: 复制代码 代码如下:function rewrite() {&nbs...

利用php绘制饼状图的实现代码

drawPieImg()函数包含8个参数,$title为饼状图的标题;$dataArr为需要显示的数据数组;$labelArr为对应数据的标签分类数组;$colorArr为对应数据的绘图...

用PHP调用Oracle存储过程的方法

但是使用存储过程至少有两个最明显的优点:速度和效率。使用存储过程的速度显然更快。在效率上,如果应用一次需要做一系列sql操作,则需要往返于php与oracle,不如把该应用直接放到数据库...

PHP iconv 函数转gb2312的bug解决方法

iconv( "UTF-8", "gb2312//IGNORE" , $FormValues['a']) ignore的意思是忽略转换时的错误,发现iconv在转换字符"—"到gb231...