Skip to content

Instantly share code, notes, and snippets.

@jaytaph
Created December 24, 2010 15:37
Show Gist options
  • Save jaytaph/754345 to your computer and use it in GitHub Desktop.
Save jaytaph/754345 to your computer and use it in GitHub Desktop.
Appending the AppendIterator
<?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;
}
<?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;
}
<?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;
}
<?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;
}
<?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;
}
}
<?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