A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP: Implementing the SeekableIterator interface - page 1
User InfoPosts
PHP: Implementing the SeekableIterator interface#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;
}

posted date: 2008-12-29 11:23:00


Re: PHP: Implementing the SeekableIterator interface#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-29 11:23:01


Re: PHP: Implementing the SeekableIterator interface#3
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.

posted date: 2008-12-29 11:42:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.