Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ContentIterator extends RecursiveIteratorIterator
- {
- private $_ordinal = 0;
- public function __construct()
- {
- parent::__construct(new RecursiveArrayIterator(func_get_args()));
- }
- public function key()
- {
- return $this->_ordinal;
- }
- public function getChildren()
- {
- $current = $this->current();
- if (property_exists($current,'array')) return new RecursiveArrayIterator(array_values($current->array()));
- throw new Exception('Shouldn't reach here!');
- }
- public function hasChildren()
- {
- $current = $this->current();
- return is_array($current) || $current instanceof Traversable;
- }
- public function next()
- {
- parent::next();
- ++$this->_ordinal;
- }
- public function rewind()
- {
- parent::rewind();
- $this->_ordinal = 0;
- }
- }
- class TestSource {
- public $array = ['a','b','c'];
- }
- foreach (new ContentIterator(new TestSource) as $key => $value)
- echo "$key => ".json_encode($value)."n";
- 0 => "a"
- 1 => "b"
- 2 => "c"
- 0 => ["a","b","c"]
Add Comment
Please, Sign In to add comment