1

Just thought I would like to share this should someone find a use for it.

Basically I needed a list of HTML colors to loop / cycle through. So I need to remove the first element of the array, place it at the end and then get the current HTML color.

Given the following array:

$colors = array(  
    "#2265fa", "#b61d1e", "#53b822", "#a9d81c", "#d6e9f5", "#43cc7d", "#e3159a", 
    "#80c85e", "#17b303", "#989240", "#014c07", "#d265f3", "#22bbb9", "#6c69a9", 
    "#7ea13a", "#0dcea2", "#99c27d", "#41405b", "#731801"
);
tshepang
  • 12,111
  • 21
  • 91
  • 136
Rudi Strydom
  • 4,417
  • 5
  • 21
  • 30

1 Answers1

2

So this is what I came up with. Sure there will be hundreds of ways to do this. This is my take on it.

# Array_shift returns the value it takes off the beginning of the array. 
# And I merely append this to the end of the array
$colors[] = array_shift($colors);

# Using current I am able to get the current first element of the array back
echo current($colors);

In this case it would be "#b61d1e" that is the current index for the array. May you find this useful somewhere.

Rudi Strydom
  • 4,417
  • 5
  • 21
  • 30
  • May not be the right place for it, but I still liked the quick and easy solution. Cheers! – oucil Apr 09 '14 at 18:10