KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

ArrayIterator

Verpakt een array in een telbaar object met indexmutatie en cursor. seek gebruikt ook bij een associatieve array de doorlooppositie vanaf nul en werpt buiten het bereik OutOfBoundsException. getArrayCopy geeft de huidige inhoud als array.

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

Syntax

new ArrayIterator($values)

Verpakt een array in een telbaar object met indexmutatie en cursor. seek gebruikt ook bij een associatieve array de doorlooppositie vanaf nul en werpt buiten het bereik OutOfBoundsException. getArrayCopy geeft de huidige inhoud 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"]

Voorbeelden

<?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);

Resultaat

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

Bronnen