php - 两个链接随机php
问题描述
我有 n 个链接,每个链接都有自己的关键字。我想一次显示两个链接,在 php.ini 中随机显示。有什么建议吗?
这是输入示例:
$text[1] = "<a href=https://www.website.ext/post-1/>words 1</a>";
$text[2] = "<a href=https://www.website.ext/post-2/>words 2</a>";
$text[3] = "<a href=https://www.website.ext/post-3/>words 3</a>";
$text[4] = "<a href=https://www.website.ext/post-4/>words 4</a>";
$text[5] = "<a href=https://www.website.ext/post-5/>words 5</a>"
....
输出示例:
words 1
words 3
or
words 5
words 2
or
words 4
words 1
解决方案
这是您可以遵循以完成任务的代码示例。您可以使用mt_rand()函数从数组中选择一个随机索引然后回显它,或者使用array_rand()函数从数组中随机提取给定数量的元素。
<?php
#example 1
$text = array("a", "b", "c", "d", "e");
$keys = array_rand($text, 2);
echo $text[$keys[0]] . "\n";
echo $text[$keys[1]] . "\n";
#example 2
$text = array("a", "b", "c", "d", "e");
echo $text[mt_rand(0,4)] . "\n";
echo $text[mt_rand(0,4)] . "\n";
?>
推荐阅读
- maps - Mapbox JS:同时 2 个叠加层(图像)
- postgresql - 是否可以在 postgresql 中使用 for 循环(功能)
- javascript - 通过索引的值从关联数组中删除一个元素
- android - 创建可扩展的网格视图
- python - 在校验和错误的情况下如何丢失尽可能少的字节
- c# - 在 NavigationView 中隐藏 AutoSuggestBox
- json - 如何在golang中对特殊的json字符串进行base64编码?
- c# - wpf getter 和 setter
- python - 如何在 Python 中制作多键击键功能
- python - python - SyntaxError:for循环块后的语法无效