Php 随机顺序的可变内容

Php 随机顺序的可变内容,php,Php,我希望每次刷新或访问页面时,这个PHP变量的内容都随机排列 比如 $test = 'a, b, c, d, e, f'; 例如,我想要随机获得它的最佳方法 $test = 'c, b, d, f, a, e'; $test = 'd, e, c, a, f, b'; 有什么解决办法吗?我建议这样做: $test = 'a, b, c, d, e, f'; $testArray = explode(', ',$test); //explode to array shuffle($testA

我希望每次刷新或访问页面时,这个PHP变量的内容都随机排列

比如

$test = 'a, b, c, d, e, f';
例如,我想要随机获得它的最佳方法

$test = 'c, b, d, f, a, e';

$test = 'd, e, c, a, f, b';

有什么解决办法吗?

我建议这样做:

$test = 'a, b, c, d, e, f';
$testArray = explode(', ',$test); //explode to array

shuffle($testArray); //shuffle it up

echo implode(', ', $testArray); //show the shuffledlyness
$content = 'a, b, c, d, e, f';
$content = explode(', ', $content);

shuffle($content);

$content = implode(', ', $content);

echo $content;

此代码的作用是:

  • a
    b
    c
    等项创建一个数组
  • 洗牌数组
  • 在每个项目之间放置一个
    ,这样我们就可以得到原始的随机字符串

    • 我建议这样做:

      $content = 'a, b, c, d, e, f';
      $content = explode(', ', $content);
      
      shuffle($content);
      
      $content = implode(', ', $content);
      
      echo $content;
      

      此代码的作用是:

      • a
        b
        c
        等项创建一个数组
      • 洗牌数组
      • 在每个项目之间放置一个
        ,这样我们就可以得到原始的随机字符串

      你自己试过什么吗?@Paul有比使用
      rand()更简单的方法。
      。数组到
      shuffle()。数组到
      shuffle()
      就是我要开始的地方。。。