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实现蛇形矩阵,回环矩阵及数字螺旋矩阵的方法。分享给大家供大家参考,具体如下: 回环矩阵指的是一个从一开始,不断按照上、右、下、左顺序依次增大的矩阵序列,例: 1...

PHP简单数据库操作类实例【支持增删改查及链式操作】

本文实例讲述了PHP简单数据库操作类。分享给大家供大家参考,具体如下: 在进行项目开发时,数据库是必不可少的东西了。但是很多时候却又对数据库SQL语句的繁杂而感到头疼。提供一个我自己使用...

PHP生成和获取XML格式数据的方法

本文实例讲述了PHP生成和获取XML格式数据的方法。分享给大家供大家参考,具体如下: 在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或...

PHP中创建和验证哈希的简单方法实探

 PHP 5.5.0 带来了一份完整的全新特性与函数的列表。全新API之一就是Password Hashing API.它包含4个函数:password_get_info(),...

php简单实现多语言切换的方法

本文实例讲述了php简单实现多语言切换的方法。分享给大家供大家参考,具体如下: 1.主程序代码: <?php include "lib/function.php"; &...