1

Im trying to achieve something simple like repeating colors for each element.

Here is my function:

function colors() {
   $colorArray = array('#f44336', '#9c27b0', '#e91e63');
   return $colorArray[array_rand($colorArray)];
}



<ul>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
    <li style="color:<?php echo colors(); ?>"><span>Word</span></li>
</ul>

this works fine, but I would like not to have random colors, instead keep repeating first 3 colors from array. I could have as many as 500 items, so the loop must be able to run infinite.

4
  • 1
    Which loop?.... Commented Sep 11, 2017 at 15:53
  • 1
    idk what you want to do? more than 3 colours? infinity amount of list items? Commented Sep 11, 2017 at 15:54
  • See please How to Ask and modify your question. Commented Sep 11, 2017 at 16:02
  • @Sebastian Tkaczyk I want 3 colors but infinite number of items. Commented Sep 11, 2017 at 16:06

4 Answers 4

1

If you want to alternate the three colors then you need to keep a record of the last color used. Here is a simple modification to your function requiring the use of a static variable:

function colors() {
    static $colorCounter = -1;
    $colorArray = array('#f44336', '#9c27b0', '#e91e63');
    $colorCounter++;
    return $colorArray[$colorCounter % count($colorArray)];
}

Here is the example output (three colors repeated infinitely): https://eval.in/859522

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

5 Comments

As long as you're setting the counter as a global variable, why not make the colors global as well to avoid the reassignment on each call?
I was going for the least amount of changes to the OPs code but that's a good point.
That makes sense.
Consider using a static variable instead - function colors() {static $counter = 0; ... } - avoids polluting global scope.
Great suggestion @NiettheDarkAbsol - updated the example.
1

Use CSS.

li:nth-child(3n+1) {color: #f44336;}
li:nth-child(3n+2) {color: #9c27b0;}
li:nth-child(3n+3) {color: #e91e63;}

Much cleaner :p

1 Comment

A great CSS alternative if the OP can live without IE8 support.
0

Create a global variable to store the index each time it is used. Increment that variable during each iteration -- if greater than the count of the array return to zero.

// Create a global variable
$current_color = -1;  

function colors() {
    // Access global var
    global $current_color;  
    // Array of colors
    $colorArray = array('#f44336', '#9c27b0', '#e91e63');  
    // Increment global var 
    $current_color++;  
    // Return to zero if var is greater than the count of your array
    $current_color = ( $current_color < count( $colorArray ) ) ? $current_color : 0;  
    // Return color
    return $colorArray[$current_color]; 
}

1 Comment

@neuromatter If your global doesn't start at -1 you will start with the second color.
0

Any excuse to have fun with InfiniteIterator

<?php
class Colors
{
    private $infinite;

    public function __construct(array $colors)
    {
        $this->infinite = new InfiniteIterator(new ArrayIterator($colors));
        $this->infinite->rewind();
    }

    public function get()
    {
        $return = $this->infinite->current();
        $this->infinite->next();

        return $return;
    }
}

$colors = new Colors(['#111', '#222', '#333']);

Just call echo $colors->get() whenever you want a color. See https://eval.in/859523

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.