CKTIS / PHP
ArrayIterator
Verpackt ein Array als veränderbares, zählbares Objekt mit Cursor. seek nimmt auch bei einer Zuordnung die Durchlaufposition ab null und wirft außerhalb OutOfBoundsException. getArrayCopy liefert den aktuellen Inhalt als Array.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
new ArrayIterator($values)Verpackt ein Array als veränderbares, zählbares Objekt mit Cursor. seek nimmt auch bei einer Zuordnung die Durchlaufposition ab null und wirft außerhalb OutOfBoundsException. getArrayCopy liefert den aktuellen Inhalt als 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"]
Beispiele
<?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);
Ergebnis
["z",7,2,{"a":4,"z":7},"OutOfBoundsException"]