我有n个链接,每个都有自己的关键字。我想一次显示两个链接,在php中随机显示。有什么建议吗?
下面是输入示例:
$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发布于 2019-04-26 16:15:06
你可以把它当作
$arr = [
'<a href=https://www.website.ext/post-1/>words 1</a>',
'<a href=https://www.website.ext/post-2/>words 2</a>',
'<a href=https://www.website.ext/post-3/>words 3</a>',
'<a href=https://www.website.ext/post-4/>words 4</a>',
'<a href=https://www.website.ext/post-5/>words 5</a>'
];
$res = array_rand($arr,2);
echo $arr[$res[0]];
echo '<br/>';
echo $arr[$res[1]];发布于 2019-04-26 16:12:00
下面是一个代码示例,您可以遵循这些代码来完成任务。您可以使用兰德()函数从数组中选择一个随机索引,然后回显它,或者使用兰德()函数从数组中随机提取给定数量的元素。
<?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";
?>https://stackoverflow.com/questions/55871203
复制相似问题