note 106034 added to class.recursiveiterator

From: Date: Tue, 04 Oct 2011 19:43:52 +0000
Subject: note 106034 added to class.recursiveiterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-183635@lists.php.net to get a copy of this message
RecursiveIterator example: <?php class MyRecursiveIterator implements RecursiveIterator { private $_data; private $_position = 0; public function __construct(array $data) { $this->_data = $data; } public function valid() { return isset($this->_data[$this->_position]); } public function hasChildren() { return is_array($this->_data[$this->_position]); } public function next() { $this->_position++; } public function current() { return $this->_data[$this->_position]; } public function getChildren() { echo '<pre>'; print_r($this->_data[$this->_position]); echo '</pre>'; } public function rewind() { $this->_position = 0; } public function key() { return $this->_position; } } $arr = array(0, 1, 2, 3, 4, 5 => array(10, 20, 30), 6, 7, 8, 9 => array(1, 2, 3)); $mri = new MyRecursiveIterator($arr); foreach ($mri as $c => $v) { if ($mri->hasChildren()) { echo "$c has children: <br />"; $mri->getChildren(); } else { echo "$v <br />"; } } ?> Result: 0 1 2 3 4 5 has children: Array ( [0] => 10 [1] => 20 [2] => 30 ) 6 7 8 9 has children: Array ( [0] => 1 [1] => 2 [2] => 3 ) ---- Server IP: 190.90.160.68 Probable Submitter: 190.28.2.183 (proxied: 190.28.2.183) ---- Manual Page -- http://www.php.net/manual/en/class.recursiveiterator.php Edit -- https://master.php.net/note/edit/106034 Del: integrated -- https://master.php.net/note/delete/106034/integrated Del: useless -- https://master.php.net/note/delete/106034/useless Del: bad code -- https://master.php.net/note/delete/106034/bad+code Del: spam -- https://master.php.net/note/delete/106034/spam Del: non-english -- https://master.php.net/note/delete/106034/non-english Del: in docs -- https://master.php.net/note/delete/106034/in+docs Del: other reasons-- https://master.php.net/note/delete/106034 Reject -- https://master.php.net/note/reject/106034 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183635) next »