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条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法