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实现的一段简单概率相关代码

本文实例讲述了php实现的一段简单概率相关代码。分享给大家供大家参考,具体如下: <?php for($i=1;$i<100000;$i++){ $x=mt_ra...

php实现支持中文的文件下载功能示例

php实现支持中文的文件下载功能示例

前言 本文主要给大家介绍了关于php实现支持中文的文件下载功能的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 问题说明 文件下载,通常有一种最为简单的方法...

详解PHP的Yii框架中扩展的安装与使用

扩展是专门设计的在 Yii 应用中随时可拿来使用的, 并可重发布的软件包。例如, yiisoft/yii2-debug 扩展在你的应用的每个页面底部添加一个方便用于调试的工具栏, 帮助你...

PHP使用JpGraph绘制折线图操作示例【附源码下载】

PHP使用JpGraph绘制折线图操作示例【附源码下载】

本文实例讲述了PHP使用JpGraph绘制折线图操作。分享给大家供大家参考,具体如下: 下载jpgraph类库,使用的是src目录下的类文件。 require_once './src...

大家都应该掌握的PHP关联数组使用技巧

在使用 PHP 进行开发的过程中,或早或晚,您会需要创建许多相似的变量,这时候你可以把数据作为元素存储在数组中。数组中的元素都有自己的 ID,因此可以方便地访问它们。 关联数组 关联数组...