php遍历CSV类实例

 2025-01-16  阅读 284  评论 8  点赞 437

摘要:本文实例讲述了php遍历csv类。分享给大家供大家参考。具体如下: filepointer = fopen( $file, 'r' ); $this->delimiter = $delimiter; } public function rewind() { $this->rowcounter = 0; rewind( $this->filepointer ); } public function current() { $this->currentel

本文实例讲述了php遍历csv类。分享给大家供大家参考。具体如下:

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程序设计有所帮助。


标签:phpphp教程

评论列表:

  •   rick.li
     发布于 3天前回复该评论
  • 写的很不错,学到了!
  •   tg2087002654
     发布于 3天前回复该评论
  • 写的很不错,学到了!
  •   danney
     发布于 2天前回复该评论
  • 写的很不错,学到了!
显示更多评论

发表评论:

管理员

承接各种程序开发,外贸网站代运营,外贸网站建设等项目
  • 内容2460
  • 积分67666
  • 金币86666

Copyright © 2024 LS'Blog-保定PHP程序员老宋个人博客 Inc. 保留所有权利。 Powered by LS'blog 3.0.3

页面耗时0.0300秒, 内存占用1.94 MB, 访问数据库29次

冀ICP备19034377号