Skip to content

Instantly share code, notes, and snippets.

@krypton
Created April 16, 2012 17:32
Show Gist options
  • Save krypton/2400119 to your computer and use it in GitHub Desktop.
Save krypton/2400119 to your computer and use it in GitHub Desktop.
Walks on the values ​​of an array value each time the function is invoked
function cycle($obj)
{
static $key = -1;
static $last_obj = array();
if(count(array_diff_assoc($obj, $last_obj)) > 0)
$key = 0;
else
$key++;
if(!array_key_exists($key, $obj)) $key = 0;
$last_obj = $obj;
return $obj[$key];
}
$array1 = array('blue', 'green', '#000', 'red');
for($i = 1; $i <= 5; $i++) {
echo "<div style='background-color: ".cycle($array1)."'>" . mt_rand() . "</div>\n";
}
$array1 = array('yellow', 'green', 'white', 'red');
for ($i = 1; $i <= 5; $i++) {
echo "<div style='background-color: ".cycle($array1)."'>" . mt_rand() . "</div>\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment