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访问RabbitMQ消息队列的方法示例

本文实例讲述了使用PHP访问RabbitMQ消息队列的方法。分享给大家供大家参考,具体如下: 扩展安装 PHP访问RabbitMQ实际使用的是AMQP协议,所以我们只要安装epel库中的...

PHP使用数组实现矩阵数学运算的方法示例

本文实例讲述了PHP使用数组实现矩阵数学运算的方法。分享给大家供大家参考,具体如下: 矩阵运算就是对两个数据表进行某种数学运算,并得到另一个数据表. 下面的例子中我们创建了一个基本完整的...

PHP入门教程之面向对象基本概念实例分析

本文实例讲述了PHP面向对象基本概念。分享给大家供大家参考,具体如下: Demo1.php <?php //怎样去创建一个类 格式:修饰符 class 类名{}...

解析 thinkphp 框架中的部分方法

 1 thinkphp 框架 中判断输入的数值和数据库中的数值是否一致    首先 需要在view文件夹下建一个模板 名为zhuce.html <...

php链式操作的实现方式分析

本文实例讲述了php链式操作的实现方式。分享给大家供大家参考,具体如下: 类似$db->where("id=1")->limit("5")->order("id des...