cycle through array





0
Date Submitted Mon. Feb. 20th, 2006 3:35 AM
Revision 1 of 1
Beginner nev3rm0re
Tags Array
Comments 0 comments
This functions cycles through array values. Can be used for creating table rows, for example.

<?php
function cycle($a) {
    static $pointer = 0;

    $values = array_values($a);
    $pointer %= count($values);
    return $values[$pointer++];
}
?>
 

<?php
    $colors = array('fff', 'ccc');
    foreach ($some_data as $data) {
        echo '<span style="background-color: '.cycle($colors).'">'.$data.'</span><br />';
    }
?>
 

German Rumm

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down