Created
April 16, 2012 17:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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