3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class IncrementIterator implements Iterator { protected $start; protected $end; protected $current; public function __construct($start, $end) { $this->start = $start; $this->end = $end; } public function rewind() { $this->current = $this->start; } public function valid() { return $this->current <= $this->end; } public function key() { return $this->current; } public function current() { return $this->current; } public function next() { $this->current++; } } class NestedIterator implements Iterator { protected $iterators = array(); protected $lastIndex = -1; protected $iteration = 0; public function addIterator(Iterator $it) { $this->iterators[] = $it; $this->lastIndex++; } public function rewind() { foreach ($this->iterators as $it) { $it->rewind(); } $this->iteration = 0; } public function valid() { return $this->lastIndex >= 0 && $this->iterators[0]->valid(); } public function key() { return $this->iteration; } public function current() { $return = array(); foreach ($this->iterators as $it) { $return[] = $it->current(); } return $return; } public function next() { $this->iteration++; for ($i = $this->lastIndex; $i >= 0; $i--) { $this->iterators[$i]->next(); if (!$this->iterators[$i]->valid()) { continue; } for ($j = $i + 1; $j <= $this->lastIndex; $j++) { $this->iterators[$j]->rewind(); } } } } $nestedIterator = new NestedIterator(); $nestedIterator->addIterator(new IncrementIterator(0, 26)); $nestedIterator->addIterator(new IncrementIterator(0, 26)); $nestedIterator->addIterator(new IncrementIterator(0, 26)); $nestedIterator->addIterator(new IncrementIterator(0, 26)); foreach ($nestedIterator as $key => $current) { list($a, $b, $c, $d) = $current; echo "[$key] => $a, $b, $c, $d\n"; }
based on IRaek
Output for 5.0.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
[0] => 0, 0, 0, 0 [1] => 1, 0, 0, 0 [2] => 2, 0, 0, 0 [3] => 3, 0, 0, 0 [4] => 4, 0, 0, 0 [5] => 5, 0, 0, 0 [6] => 6, 0, 0, 0 [7] => 7, 0, 0, 0 [8] => 8, 0, 0, 0 [9] => 9, 0, 0, 0 [10] => 10, 0, 0, 0 [11] => 11, 0, 0, 0 [12] => 12, 0, 0, 0 [13] => 13, 0, 0, 0 [14] => 14, 0, 0, 0 [15] => 15, 0, 0, 0 [16] => 16, 0, 0, 0 [17] => 17, 0, 0, 0 [18] => 18, 0, 0, 0 [19] => 19, 0, 0, 0 [20] => 20, 0, 0, 0 [21] => 21, 0, 0, 0 [22] => 22, 0, 0, 0 [23] => 23, 0, 0, 0 [24] => 24, 0, 0, 0 [25] => 25, 0, 0, 0 [26] => 26, 0, 0, 0
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/ALJEL on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/ALJEL on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/ALJEL on line 2
Process exited with code 255.