php8.5
Home/ Manual/ spl / splobjectstorage/ SplObjectStorage::seek

SplObjectStorage::seek

PHP function Edit on GitHub ✎

(PHP 8 >= 8.4.0)

Seeks iterator to a position

Description

SplObjectStorage::seek(int $offset): void

Seeks to a given position in the iterator.

Parameters

offset

The position to seek to.

Return Values

Void

Errors/Exceptions

Throws an OutOfBoundsException if the offset is not seekable.

Examples

SplObjectStorage::seek example

Seeks to item position 2 in the iterator.

php
<?php
class Test {
    public function __construct(public string $marker) {}
}

$a = new Test("a");
$b = new Test("b");
$c = new Test("c");

$storage = new SplObjectStorage();
$storage[$a] = "first";
$storage[$b] = "second";
$storage[$c] = "third";

$storage->seek(2);
var_dump($storage->key());
var_dump($storage->current());
?>

The above example will output:

output
int(2)
object(Test)#3 (1) {
  ["marker"]=>
  string(1) "c"
}

See Also

  • SeekableIterator

Source: reference/spl/splobjectstorage/seek.xml · from the official PHP manual (php/doc-en)