使用数组将CSS类分配给tagcloud
我想知道是否有其他方法可以做这样的事情:
$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>";
}
问题是,当我使用这种方法时,同一个 a.class 会被多次选择,而有些类根本不会被选择。
我希望使用 tag1 到 tag10,并且在每 10 个都被取完之前不会选择同一类两次。
有人知道我怎样才能实现这个目标吗?
感谢您的所有回复!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已经使用
shuffle
随机化了类的顺序,因此没有理由再次随机化它们 - 只需按顺序循环即可Since you're already randomizing the order of classes with
shuffle
, there's no reason to randomize them again - just loop through in order