douwen9540 2019-04-26 15:59
浏览 86
已采纳

两个链接随机php [重复]

This question already has an answer here:

I have n number of links, each with its own keyword. I would like to show two links at a time, randomly in php. Any suggestion?

Here is input example :

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

....

output example :

words 1

words 3

or

words 5

words 2

or

words 4

words 1
</div>
  • 写回答

2条回答 默认 最新

  • douwen9343 2019-04-26 16:15
    关注

    You can approach this as

     $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]];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?