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

PHP traverses CSV class instances_PHP tutorial

WBOY
Release: 2016-07-13 09:57:18
Original
812 people have browsed it

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...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template