Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Standard PHP Library (SPL) Functions : ArrayIterator::seek

ArrayIterator::seek

Seek to position ()
void ArrayIterator::seek ( int position )


Code Examples / Notes » arrayiterator_seek

adar

Nice way to get previous and next keys:
<?php
class myIterator extends ArrayIterator{
   private $previousKey;
   public function __construct( $array ) {
       parent::__construct( $array );
       $this->previousKey = NULL;
   }
   public function getPreviousKey() {
       return $this->previousKey;
   }
   public function next() {
       $this->previousKey = $this->key();
       parent::next();
   }
   public function getNextKey() {
       $key = $this->current()-1;
       parent::next();
       if ( $this->valid() ) {
           $return = $this->key();
       } else {
           $return = NULL;
       }
       $this->seek( $key );
       return $return;
   }
}
class myArrayObject extends ArrayObject {
   private $array;
   public function __construct( $array ) {
       parent::__construct( $array );
       $this->array = $array;
   }
   public function getIterator() {
       return new myIterator( $this->array );
   }
}
?>
And for testing:
<?php
$array['a'] = '1';
$array['b'] = '2';
$array['c'] = '3';
$arrayObject = new myArrayObject( $array );
for ( $i = $arrayObject->getIterator() ; $i->valid() ; $i->next() ) {
   print "iterating...\n";
   print "prev: ".$i->getPreviousKey()."\n";
   print "current: ".$i->key()."\n";
   print "next: ".$i->getNextKey()."\n";
}
?>
Output will be:
iterating...
prev:
current: a
next: b
iterating...
prev: a
current: b
next: c
iterating...
prev: b
current: c
next:


Change Language


Follow Navioo On Twitter
ArrayIterator::current
ArrayIterator::key
ArrayIterator::next
ArrayIterator::rewind
ArrayIterator::seek
ArrayIterator::valid
ArrayObject::append
ArrayObject::__construct
ArrayObject::count
ArrayObject::getIterator
ArrayObject::offsetExists
ArrayObject::offsetGet
ArrayObject::offsetSet
ArrayObject::offsetUnset
CachingIterator::hasNext
CachingIterator::next
CachingIterator::rewind
CachingIterator::__toString
CachingIterator::valid
CachingRecursiveIterator::getChildren
CachingRecursiveIterator::hasChildren
DirectoryIterator::__construct
DirectoryIterator::current
DirectoryIterator::getATime
DirectoryIterator::getCTime
DirectoryIterator::getFilename
DirectoryIterator::getGroup
DirectoryIterator::getInode
DirectoryIterator::getMTime
DirectoryIterator::getOwner
DirectoryIterator::getPath
DirectoryIterator::getPathname
DirectoryIterator::getPerms
DirectoryIterator::getSize
DirectoryIterator::getType
DirectoryIterator::isDir
DirectoryIterator::isDot
DirectoryIterator::isExecutable
DirectoryIterator::isFile
DirectoryIterator::isLink
DirectoryIterator::isReadable
DirectoryIterator::isWritable
DirectoryIterator::key
DirectoryIterator::next
DirectoryIterator::rewind
DirectoryIterator::valid
FilterIterator::current
FilterIterator::getInnerIterator
FilterIterator::key
FilterIterator::next
FilterIterator::rewind
FilterIterator::valid
LimitIterator::getPosition
LimitIterator::next
LimitIterator::rewind
LimitIterator::seek
LimitIterator::valid
ParentIterator::getChildren
ParentIterator::hasChildren
ParentIterator::next
ParentIterator::rewind
RecursiveDirectoryIterator::getChildren
RecursiveDirectoryIterator::hasChildren
RecursiveDirectoryIterator::key
RecursiveDirectoryIterator::next
RecursiveDirectoryIterator::rewind
RecursiveIteratorIterator::current
RecursiveIteratorIterator::getDepth
RecursiveIteratorIterator::getSubIterator
RecursiveIteratorIterator::key
RecursiveIteratorIterator::next
RecursiveIteratorIterator::rewind
RecursiveIteratorIterator::valid
SimpleXMLIterator::current
SimpleXMLIterator::getChildren
SimpleXMLIterator::hasChildren
SimpleXMLIterator::key
SimpleXMLIterator::next
SimpleXMLIterator::rewind
SimpleXMLIterator::valid
class_implements
class_parents
iterator_count
iterator_to_array
spl_autoload_call
spl_autoload_extensions
spl_autoload_functions
spl_autoload_register
spl_autoload_unregister
spl_autoload
spl_classes
spl_object_hash
eXTReMe Tracker