note 89424 added to class.iterator
| From: | jwvdveer at gmail dot com | Date: | Sat, 07 Mar 2009 12:26:21 +0000 |
| Subject: | note 89424 added to class.iterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151581@lists.php.net to get a copy of this message | ||
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";
}
?>
----
Server IP: 83.137.20.135
Probable Submitter: 84.81.161.35
----
Manual Page -- http://www.php.net/manual/en/class.iterator.php
Edit -- https://master.php.net/note/edit/89424
Del: integrated -- https://master.php.net/note/delete/89424/integrated
Del: useless -- https://master.php.net/note/delete/89424/useless
Del: bad code -- https://master.php.net/note/delete/89424/bad+code
Del: spam -- https://master.php.net/note/delete/89424/spam
Del: non-english -- https://master.php.net/note/delete/89424/non-english
Del: in docs -- https://master.php.net/note/delete/89424/in+docs
Del: other reasons-- https://master.php.net/note/delete/89424
Reject -- https://master.php.net/note/reject/89424
Search -- https://master.php.net/manage/user-notes.php