Question

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!

Was it helpful?

Solution

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>"; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top