note 89424 deleted from class.iterator by danbrown

From: Date: Mon, 11 May 2009 18:48:23 +0000
Subject: note 89424 deleted from class.iterator by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154466@lists.php.net to get a copy of this message
Note Submitter: jwvdveer at gmail dot com ---- Remark that floats aren't supported as a key in the Iteratorclass as they aren't in arrays. <?php class someIterator implements Iterator { protected $data = Array(Array(1.2, 1), Array(5.6, 3)); protected $pointer = 0; public function key(){ return $this->data[$this->pointer][0]; } public function current(){ return $this->data[$this->pointer][1]; } public function next(){ ++$this->pointer; return $this->current(); } public function valid(){ return (($this->pointer >= 0) && ($this->pointers < count($this->data)); } public function rewind(){ $this->pointer = 0; } } $i = new someIterator; foreach($i as $x => $y) echo $x.': '.$y."\r\n"; ?> Puts something out like this: 1: 1 5: 3 So the mechanisme foreach uses for floats is actually this: <?php $it=new SomeIterator(); for( $it->rewind(); $it->valid(); $y=$it->current(), $x=intval($it->key()), $it->next() ) { print $x.': '.$y."\r\n"; } ?>

« previous php.notes (#154466) next »