首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >双链路随机php

双链路随机php
EN

Stack Overflow用户
提问于 2019-04-26 15:59:48
回答 2查看 121关注 0票数 0

我有n个链接,每个都有自己的关键字。我想一次显示两个链接,在php中随机显示。有什么建议吗?

下面是输入示例:

代码语言:javascript
复制
$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>"

....

输出示例:

代码语言:javascript
复制
words 1

words 3

or

words 5

words 2

or

words 4

words 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-26 16:15:06

你可以把它当作

代码语言:javascript
复制
 $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]];
票数 0
EN

Stack Overflow用户

发布于 2019-04-26 16:12:00

下面是一个代码示例,您可以遵循这些代码来完成任务。您可以使用兰德()函数从数组中选择一个随机索引,然后回显它,或者使用兰德()函数从数组中随机提取给定数量的元素。

代码语言:javascript
复制
<?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";

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55871203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档