PHP Doku:: Seek to position - arrayiterator.seek.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)IteratorenThe ArrayIterator classArrayIterator::seek

Ein Service von Reinhard Neidl - Webprogrammierung.

The ArrayIterator class

<<ArrayIterator::rewind

ArrayIterator::serialize>>

ArrayIterator::seek

(PHP 5 >= 5.0.0)

ArrayIterator::seekSeek to position

Beschreibung

void ArrayIterator::seek ( int $position )
Warnung

Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfügung.

Parameter-Liste

position

The position to seek to.

Rückgabewerte

Es wird kein Wert zurückgegeben.


2 BenutzerBeiträge:
- Beiträge aktualisieren...
jon at ngsthings dot com
22.10.2008 2:08
<?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
27.03.2007 9:54
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:



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)