note 57803 added to language.oop5.iterations

From: Date: Fri, 14 Oct 2005 15:06:38 +0000
Subject: note 57803 added to language.oop5.iterations
Groups: php.notes 
Request: Send a blank email to php-notes+get-97022@lists.php.net to get a copy of this message
Hier is an example for a CSV Iterator. <?php class CsvIterator implements Iterator { const ROW_SIZE = 4096; /** * The pointer to the cvs file. * @var resource * @access private */ private $filePointer = null; /** * The current element, which will * be returned on each iteration. * @var array * @access private */ private $currentElement = null; /** * The row counter. * @var int * @access private */ private $rowCounter = null; /** * The delimiter for the csv file. * @var str * @access private */ private $delimiter = null; /** * This is the constructor.It try to set the delimiter * and open the csv file.The method throws an exception * on failure. * * @access public * @param str $file The csv file. * @param str $delimiter The delimiter. * * @throws Exception */ public function __construct($file, $delimiter=',') { try { $this->filePointer = fopen($file, 'r'); $this->delimiter = $delimiter; } catch (Exception $e) { throw new Exception('The file "'.$file.'" cannot be read.'); } } /** * This method resets the file pointer. * * @access public */ public function rewind() { $this->rowCounter = 0; rewind($this->filePointer); } /** * This method returns the current csv row as a 2 dimensional array * * @access public * @return array The current csv row as a 2 dimensional array */ public function current() { $this->currentElement = fgetcsv($this->filePointer, self::ROW_SIZE, $this->delimiter); $this->rowCounter++; return $this->currentElement; } /** * This method returns the current row number. * * @access public * @return int The current row number */ public function key() { return $this->rowCounter; } /** * This method checks if the end of file is reached. * * @access public * @return boolean Returns true on EOF reached, false otherwise. */ public function next() { return !feof($this->filePointer); } /** * This method checks if the next row is a valid row. * * @access public * @return boolean If the next row is a valid row. */ public function valid() { if (!$this->next()) { fclose($this->filePointer); return false; } return true; } } ?> Usage : <?php $csvIterator = new CsvIterator('/path/to/csvfile.csv'); foreach ($csvIterator as $row => $data) { // do somthing with $data } ?> ---- Manual Page -- http://www.php.net/manual/en/language.oop5.iterations.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57803 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57803&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57803&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57803&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57803&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57803&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57803&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57803&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57803&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97022) next »