CakeFest 2025 Madrid: The Official CakePHP Conference

Voting

: three minus three?
(Example: nine)

The Note You're Voting On

Venelin Vulkov
16 years ago
Another fine Iterator from php . You can use it especially when you have to iterate over objects

<?php
$fruits
= array(
"apple" => "yummy",
"orange" => "ah ya, nice",
"grape" => "wow, I love it!",
"plum" => "nah, not me"
);
$obj = new ArrayObject( $fruits );
$it = $obj->getIterator();

// How many items are we iterating over?

echo "Iterating over: " . $obj->count() . " values\n";

// Iterate over the values in the ArrayObject:
while( $it->valid() )
{
echo
$it->key() . "=" . $it->current() . "\n";
$it->next();
}

// The good thing here is that it can be iterated with foreach loop

foreach ($it as $key=>$val)
echo
$key.":".$val."\n";

/* Outputs something like */

Iterating over: 4 values
apple
=yummy
orange
=ah ya, nice
grape
=wow, I love it!
plum=nah, not me

?>

Regards.

<< Back to user notes page

To Top