Home >Backend Development >PHP Tutorial >PHP traverses CSV class instances_PHP tutorial
The details are as follows:
4 11 12 |
<๐>class CSSVIterator 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 ?> |