1

I'm wondering if there is a function built into PHP that I could leverage to loop through an array, and reset to the beginning to continue to loop again.

The use of this would be an array of colors for an SVG that is created with a PHP function. I think my max case would be X but I want to make sure if I have more than X I restart with the color codes.

Below is the code I have that works, but wondering if there is a built in function to do this.

$color_array = array( 1 => '#00cc00', 2=> '#B45F04', 3=> '#0101DF', 4=> '#B40486', 5=> 'F1F105', 6=>'F10505');
$num_color_array = count($color_array); //get number of elements

foreach(loop through array 1){ //psuedo code
    $array_color_index = 1; 

    foreach(loop throguh array 2){ //psuedo code
        if($array_color_index > $num_color_array){ 
            $array_color_index = 1; //if > num elements reset
        }
        $color_fill = $color_array[$array_color_index]; //pull the color code

        fill:'.$color_fill.' //use the color code here...simplified for example...

        $array_color_index++; //increment index     
    }
}
2
  • 6
    Sounds like the perfect situation for a while loop Commented Jan 14, 2015 at 15:55
  • I was afraid using a while loop would take more processing power than what I outlined above. Commented Jan 14, 2015 at 19:08

3 Answers 3

3

You could use something like this (using the modulus):

$color_array = array('#f00', '#0f0', '#00f');
$elements = get_some_colorable_elements();
// For each element in $elements, the modulus returns a value between 0 and the size of $color_array
for ($i = 0; $i < count($elements); $i += 1) {
  $colorForElement = $color_array[$i % count($color_array)];
  fill_color_for_element($colorForElement);
}

As far as I know there is no built in function specifically for this purpose (other than the modulus).

Sign up to request clarification or add additional context in comments.

Comments

0

Try using the modulus:

$color_fill = $color_array[$array_color_index % $num_color_array];

You're getting the remainder of your index divided by the total number of elements.. so when the index = number of elements, remainder = 0, and then it cycles.

Comments

0

If you loop through an array and then start at the beginning again using anything like a foreach() you're essentially creating an infinite loop. I don't think there's a basic function for it, but it's fairly easy to make one. Of course, you'd have to use BREAK to end it.

A function like this would do (it's a Generator, so you need an up to date PHP version)

function constantLoop( $array ) {
  while(true) {
    foreach( $array as $element )  {
      yield $element;
    }
  }  
}

You can use it like this:

foreach( constantLoop( $array ) as $value );

But you HAVE to break, because as the name says, it'll keep looping forever.

1 Comment

I should have called out specifically that the two foreach loops are going through different arrays than the color array, so they will terminate on their own.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.