ArrayIterator
在线手册:中文  英文

ArrayIterator::seek

(PHP 5 >= 5.0.0)

ArrayIterator::seekSeek to position

说明

public void ArrayIterator::seek ( int $position )
Warning

本函数还未编写文档,仅有参数列表。

参数

position

The position to seek to.

返回值

没有返回值。


ArrayIterator
在线手册:中文  英文

用户评论:

php at mattjanssen dot com (2011-05-12 08:53:54)

If you call ->seek() on a position that doesn't exist, you'll get the following error:
PHP Fatal error: Uncaught exception 'OutOfBoundsException' with message 'Seek position 10 is out of range' in script.php:1

jon at ngsthings dot com (2008-10-21 17:08:20)

<?php
// didn't see any code demos...here's one from an app I'm working on

$array = array('1' => 'one',
               
'2' => 'two',
               
'3' => 'three');

$arrayobject = new ArrayObject($array);
$iterator $arrayobject->getIterator();

if(
$iterator->valid()){
    
$iterator->seek(1);            // expected: two, output: two
    
echo $iterator->current();    // two
}

?>

adar at darkpoetry dot de (2007-03-26 23:54:33)

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:

易百教程