加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php 遍历CSV的类用法示例

发布时间:2020-05-25 04:58:58 所属栏目:PHP 来源:互联网
导读:php 遍历CSV的类用法示例

感兴趣的小伙伴,下面一起跟随脚本之家 jb51.cc的小编来看看吧。
经测试代码如下:


<?php
/**
 * 遍历CSV类
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
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
?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读