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技术开发技巧分享

1. 提高PHP的运行效率   PHP的优点之一是速度很快,对于一般的网站应用,可以说是已经足够了。不过如果站点的访问量很高、带宽窄或者其它的因素令服务器产生性能瓶颈的时候,你可能得想想...

使用Codeigniter重写insert的方法(推荐)

使用Codeiginter 框架插入数据时有唯一索引键值存在解决办法 对数据进行存储的时候,会有一些唯一索引的字段已经有值了,插入数据的时候会失败我们通常解决办法是先查询这个值是否存在,...

php比较多维数组中值的大小排序实现代码

如果值没有重复的情况,可以先用array_flip()来交换键和值,然后krsort(),最后再array_flip()交换回来,就可以比较大小了。如果要截取数组,可用array_sli...

PHP伪静态页面函数附使用方法

function MakeUrl($arr){           &nbs...

PHP-FPM运行状态的实时查看及监控详解

PHP-FPM运行状态的实时查看及监控详解

前言 大家都知道PHP-FPM内置了状态页,开启后可查看PHP-FPM的详细运行状态,给PHP-FPM优化带来帮助。 打开php-fpm.conf,配置php-fpm状态页选项 p...