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 /** file: image.class.php 类名...

PHP排序算法类实例

本文实例讲述了PHP排序算法类。分享给大家供大家参考。具体如下: 四种排序算法的PHP实现: 1) 插入排序(Insertion Sort)的基本思想是: 每次将一个待排序的记录,按其...

PHP 杂谈《重构-改善既有代码的设计》之三 重新组织数据

PHP 杂谈《重构-改善既有代码的设计》之三 重新组织数据

思维导图 介绍    承接上文的PHP 杂谈《重构-改善既有代码的设计》之 重新组织你的函数继续重构方面的内容。   这章主要针对数据的重构。 &nbs...

php 使用expat方式解析xml文件操作示例

本文实例讲述了php 使用expat方式解析xml文件操作。分享给大家供大家参考,具体如下:test.xml:<?xml version="1.0"&n...

详解PHP中的Traits

PHP是单继承的语言,在PHP 5.4 Traits出现之前,PHP的类无法同时从两个基类继承属性或方法。php的Traits和Go语言的组合功能类似,通过在类中使用use关键字声明要组...