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$arr['laruence'] = 'huixinchen';$arr['yahoo']    = 2007;$arr['baidu']...

PHP7导出Excel报ERR_EMPTY_RESPONSE解决方法

PHP7导出Excel报ERR_EMPTY_RESPONSE解决方法

PHP在本机可以正常导出Excel,在上Linux就提示ERR_EMPTY_RESPONSE,下面说一下怎么不降版本还能正常导出Excel,解决上面的问题。 使用导出Excel的时候显示...

PHP伪静态Rewrite设置之APACHE篇

 一、Apache配置:   1、支持httpd.conf 配置 2、支持目录 .htaccess配置(一种"分布式配置"文件针对虚拟空间,空间商不让修改Apache...

PHP全局变量与超级全局变量区别分析

本文分析了PHP全局变量与超级全局变量区别。分享给大家供大家参考,具体如下: 全局变量就是在函数外面定义的变量。不能在函数中直接使用。因为它的作用域不会到函数内部。所以在函数内部使用的时...

PHP IDE PHPStorm配置支持友好Laravel代码提示方法

PHP IDE PHPStorm配置支持友好Laravel代码提示方法

PHPStorm神器可以支持更友好的laravel框架代码提示(点击查看),只需要执行如下才做: 第一步:在项目的composer.json中添加如下一行 复制代码 代码如下: "req...