PHP 8.5.0 Alpha 4 available for testing

Voting

: two plus zero?
(Example: nine)

The Note You're Voting On

Vuong Nguyen
7 years ago
You can easily realise that ArrayObject can use various functions as they are in ArrayIterator to iterate an object-as-a-array. However, you need to "activate" these function (rewind, valid, next and so on...) by using getIterator() first. Actually this function inherits from Iterator Aggregate interface.

Take a look at the following basic example. The results are the same:

<?php

$array
= [1, 2, 3, 4];
$a = new ArrayObject($array);
$b = new ArrayIterator($array);

$iterator = $a->getIterator();

for(
$iterator->rewind(); $iterator->valid(); $iterator->next()){
echo
$iterator->current()*2;

}

for(
$b->rewind(); $b->valid(); $b->next()){
echo
$b->current()*2;

}

//Resulst are the same 2468 AND 2468

<< Back to user notes page

To Top