php遍历CSV类实例

yipeiwu_com6年前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实现二维数组根据key进行排序的方法

本文实例讲述了PHP实现二维数组根据key进行排序的方法。分享给大家供大家参考,具体如下: 在PHP中内置了很多对数组进行处理的函数,有很多时候我们直接使用其内置函数就能达到我们的需求,...

浅谈本地WAMP环境的搭建

在php本地搭建过程中,php环境配置是至关重要的一部分,本文就php在本地的环境配置中作简要说明。以供大家学习! 配置Windows+Apache+Mysql+PHP开发运行环境 安装...

Nginx环境下PHP flush失效的解决方法

问题 我在一次调试PHP逐行输出时候,发现ob_flush和flush两个都失效了,通过phpinfo基本能判断php.ini的设置是正常的。 解决方法 再去看Nginx,发现Nginx...

php中3des加密代码(完全与.net中的兼容)

复制代码 代码如下: <?php class Crypt3Des { private $key = ""; private $iv = ""; /** * 构造,传递二个已经进行b...

Apache2 httpd.conf 中文版

Apache2 httpd.conf 中文版  # # 基于 NCSA 服务的配置文件。 # #这是Apache服务器主要配置...