使用数组将CSS类分配给tagcloud

发布于 2024-12-11 15:12:40 字数 486 浏览 1 评论 0原文

我想知道是否有其他方法可以做这样的事情:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2024-12-18 15:12:40

由于您已经使用 shuffle 随机化了类的顺序,因此没有理由再次随机化它们 - 只需按顺序循环即可

$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>"; 
}

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>"; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文