Last active
December 26, 2015 18:19
-
-
Save Rudis1261/7193639 to your computer and use it in GitHub Desktop.
Cycling through arrays without looping through it
This file contains hidden or 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
<?php | |
/* | |
I was faced with a problem where I had to cycle through a list of HTML colors and I thought this was | |
a nice way of doing it. As array_shift returns the value you remove at the front, simply appending it | |
to the array does the business. I thought I would share it in-case someone finds it useful. | |
*/ | |
# We need a list of colors for grouping purposes | |
$colors = array( | |
"#2265fa", "#b61d1e", "#53b822", "#a9d81c", "#d6e9f5", "#43cc7d", "#e3159a", | |
"#80c85e", "#17b303", "#989240", "#014c07", "#d265f3", "#22bbb9", "#6c69a9", | |
"#7ea13a", "#0dcea2", "#99c27d", "#41405b", "#731801" | |
); | |
# Move first element in array to the last | |
$colors[] = array_shift($colors); | |
echo current($colors); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment