Created
December 24, 2010 15:37
-
-
Save jaytaph/754345 to your computer and use it in GitHub Desktop.
Appending the AppendIterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Define arrays | |
$array1 = new ArrayIterator( array('php', 'perl', 'python', 'c') ); | |
$array2 = new ArrayIterator( array('singleton', 'factory', 'strategy') ); | |
$array3 = new ArrayIterator( array('yellow', 'blue', 'red', 'white') ); | |
// Create iterator and add all arrays to it | |
$iterator = new AppendIterator(); | |
$iterator->append($array1); | |
$iterator->append($array2); | |
$iterator->append($array3); | |
foreach($iterator as $key => $item) { | |
echo $key . ' = '.$item . PHP_EOL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Define arrays | |
$array1 = new ArrayIterator( array('php', 'perl', 'python', 'c') ); | |
$array2 = new ArrayIterator( array('singleton', 'factory', 'strategy') ); | |
$array3 = new ArrayIterator( array('yellow', 'blue', 'red', 'white') ); | |
// Create iterator and add all arrays to it | |
$iterator = new MergeIterator(); | |
$iterator->append($array1); | |
$iterator->append($array2); | |
$iterator->append($array3); | |
foreach($iterator as $key => $item) { | |
echo $key . ' = '. $item . PHP_EOL; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Define arrays | |
$array1 = new ArrayIterator( array('php', 'perl', 'python', 'c') ); | |
$array2 = new ArrayIterator( array('singleton', 'factory', 'strategy') ); | |
$array3 = new ArrayIterator( array('yellow', 'blue', 'red', 'white') ); | |
// Create iterator and add all arrays to it | |
$iterator = new MergeIterator(); | |
$iterator->append($array1); | |
$iterator->append($array2); | |
$iterator->append($array3); | |
foreach($iterator as $key => $item) { | |
echo $iterator->getIteratorIndex() . ' ' . | |
$key . ' = '. $item . PHP_EOL; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Define arrays | |
$array1 = new ArrayIterator( array('php', 'perl', 'python', 'c') ); | |
$array2 = new ArrayIterator( array('singleton', 'factory', 'strategy') ); | |
$array3 = new ArrayIterator( array('yellow', 'blue', 'red', 'white') ); | |
// Create iterator and add all arrays to it | |
$iterator = new MergeIterator(); | |
$iterator->append('languages', $array1); | |
$iterator->append('patterns', $array2); | |
$iterator->append('colors', $array3); | |
foreach($iterator as $key => $item) { | |
echo $iterator->group() . ' : ' . $key . ' = '. $item . PHP_EOL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MergeIterator extends AppendIterator { | |
protected $_position; | |
public function next() { | |
parent::next(); | |
$this->_position++; | |
} | |
public function rewind() { | |
parent::rewind(); | |
$this->_position = 0; | |
} | |
public function key() { | |
return $this->_position; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MergeIterator extends AppendIterator { | |
protected $_position = 0; | |
protected $_groups = array(); | |
public function append ($group, $iterator) { | |
parent::append($iterator); | |
$this->_groups[] = $group; | |
} | |
public function group() { | |
return $this->_groups[$this->getIteratorIndex()]; | |
} | |
public function next() { | |
parent::next(); | |
$this->_position++; | |
} | |
public function rewind() { | |
parent::rewind(); | |
$this->_position = 0; | |
} | |
public function key() { | |
return $this->_position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment