Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

ArrayIterator

Wrap an array in an object that supports indexed mutation, counting and iterator methods. seek takes a zero-based traversal position even for an associative array and throws OutOfBoundsException outside the available positions. getArrayCopy returns the iterator contents as an array.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new ArrayIterator($values)

Wrap an array in an object that supports indexed mutation, counting and iterator methods. seek takes a zero-based traversal position even for an associative array and throws OutOfBoundsException outside the available positions. getArrayCopy returns the iterator contents as an array.

$it=new ArrayIterator(["a"=>4,"z"=>6]); $it["z"]=7; $it->seek(1);
$result=[$it->key(),$it->current(),count($it),$it->getArrayCopy()];
try{$it->seek(9);}catch(OutOfBoundsException $e){$result[]=get_class($e);}
echo json_encode($result);
["z",7,2,{"a":4,"z":7},"OutOfBoundsException"]

Examples

<?php
$it=new ArrayIterator(["a"=>4,"z"=>6]); $it["z"]=7; $it->seek(1);
$result=[$it->key(),$it->current(),count($it),$it->getArrayCopy()];
try{$it->seek(9);}catch(OutOfBoundsException $e){$result[]=get_class($e);}
echo json_encode($result);

Result

["z",7,2,{"a":4,"z":7},"OutOfBoundsException"]

Sources