Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How i can show one array in each loop? for example with this code i can show random color codes for each loop, but i need to show order by DESC:

$Colors = array('111', '222', '333', '444', '555');

$i = 1;

while($i <= $Limit){
  echo "Color Code:". $Colors[array_rand($Colors)]."<br>";
$i++;
}

and this is my result and problem:

Color Code:333
Color Code:333
Color Code:444
Color Code:111
Color Code:555
Color Code:222
Color Code:111
Color Code:222
Color Code:555
Color Code:222
Color Code:333
Color Code:444

how i can show the result like this:

Color Code:111
Color Code:222
Color Code:333
Color Code:444
Color Code:555
Color Code:111
Color Code:222
Color Code:333
Color Code:444
Color Code:555
Color Code:111
Color Code:222
...
share|improve this question

closed as unclear what you're asking by jeroen, Lashane, Bart, Christoph, Toby Allen Mar 8 '14 at 21:34

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2  
What in the world does this code do? –  Maciej Sz Dec 10 '13 at 20:40
    
You are picking random entries from a sequential array and want to list them sequentially? –  AbraCadaver Dec 10 '13 at 20:41
    
Not Random, sequentially –  Alireza Dec 10 '13 at 20:42
    
You shown us two different arrays of which one is undefined here, and the other one isn't used... –  Maciej Sz Dec 10 '13 at 20:43
1  
The obvious followup question is why are you randomizing them initially then? –  Explosion Pills Dec 10 '13 at 20:43

3 Answers 3

up vote 1 down vote accepted
while($i <= $Limit){
    echo "Color Code:". $input[$i % count($input)]."<br>";
    $i++;
}

Or substitute $input for $Colors depending upon what the variable is actually called.

share|improve this answer
    
@MaciejSz you could move count outside the loop; technically more efficient but I don't think it would be noticeable for 20 items. –  Explosion Pills Dec 10 '13 at 20:46
    
Not until two years after, when unnoticeably that array grow to 2M items... I'm just saying: it should be automatic. –  Maciej Sz Dec 10 '13 at 20:50
    
@Explosion Pills thank you my friend –  Alireza Dec 10 '13 at 20:53
2  
@MaciejSz time php -r '$x = array("1", "2", "3", "4", "5"); $i = 0; $limit = 2000000; while ($i <= $limit) { count($x); $i++; }' -- takes about 32 MS on my machine. –  Explosion Pills Dec 10 '13 at 20:54
1  
Php 5.2 - 300ms; PHP 5.4 - 600ms; PHP 5.5 - 1s (!) What do you have there? Like 4 Xeons? –  Maciej Sz Dec 10 '13 at 21:07
<?php
$Colors = array('222', '111', '333', '555', '444');
sort($Colors);

for($i = 0; $i < count($Colors); $i++)
{
   printf("Color code: %s <br />", $Colors[$i]);
}
?>
share|improve this answer
    
It's a small static array in a very simplistic example, not sure how thread-safety would really be an issue here. The only problem I see would be if they are pushing values into the array they are iterating over, which they shouldn't be and are not in their question. –  Rob M. Dec 10 '13 at 20:52
    
thank you my friend. –  Alireza Dec 10 '13 at 21:00

This should do the trick!

for($i = 0; $i < sizeof($Colors); $i++){
  echo "Color Code:". $Colors[$i]."<br>";
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.