0

I am wondering if there are any other approach to do something like this:

$classes = array("tag5", "tag2", "tag9", "tag4", "tag1", "tag6", "tag10", "tag8", "tag3", "tag7" );
shuffle($tags); 
foreach ($tags as $tag) { 
    $class = $classes[array_rand($classes)];
    echo "<li><a href='#' class='".$class."'> ".$tag."</a></li>"; 
}

The thing is, when I use this approach, the same a.class gets picked multiple times, and some classes do not get picked at all.

I am looking to use tag1 trough tag10, and not picking the same class twice until every 10 has been taken.

Anyone know how I can acchive this?

Thanks for any and all replies!

1
  • 1
    isn't shuffle sufficient to randomize the array? it will give you a random order with no repeats Commented Oct 22, 2011 at 15:35

1 Answer 1

2

Since you're already randomizing the order of classes with shuffle, there's no reason to randomize them again - just loop through in order

$classes = array("tag5", "tag2", "tag9", "tag4", "tag1", "tag6", "tag10", "tag8", "tag3", "tag7" );
shuffle($tags); 
foreach ($tags as $i => $tag) { 
    $class = $classes[$i % count($classes)];
    echo "<li><a href='#' class='".$class."'> ".$tag."</a></li>"; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Tom: Welcome to SO. Please accept the answer. This will mark your question as answered and is how this site works. See as well: meta.stackexchange.com/q/5234/147909 - Thanks!

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.