views:

267

answers:

1

I'm currently writing a class that implements the SeekableIterator interface and have run into a problem. I have two internal arrays that I am using and would like to allow for iteration through both of them from outside the class. Is there an easy way to do this without first merging the two arrays within the class? Here's a quick example of what I'm trying to do:

class BookShelf implements ArrayAccess, Countable, SeekableIterator {
    protected $_books = array(...);
    protected $_magazines = array(...);

    /**** CLASS CONTENT HERE ****/
}

$shelf = new BookShelf();

// Loops through both arrays, first books (if any) and then magazines (if any)
foreach($shelf as $item) {
    echo $item;
}
A: 

Assuming those arrays are both numerically indexed, if the current index is smaller than

count($this->_books);

then return

$this->_books[$index];

Otherwise, if the index is smaller than count(books)+count(magazines), return

$this->_magazines[$index-count($this->_books)]

Failing both, an OutOfBoundsException may be in order.

Everything else should just fall into place.

Waquo