Home >Backend Development >PHP Tutorial >PHP traverses CSV class instances_PHP tutorial

PHP traverses CSV class instances_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:18808browse

php traverses CSV class instances

The details are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

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

?>

1 2 3

4

6 7 8 9 10
11 12
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<๐ŸŽœ>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 ?>
http://www.bkjia.com/PHPjc/983320.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/983320.htmlTechArticlephp traverses the CSV class instance as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 ?php class CSSVIterator...
Statement๏ผš
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn