1

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

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

我想要随机获取它的最佳方法,例如

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

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

有什么解决办法吗?

4

2 回答 2

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

shuffle($testArray); //shuffle it up

echo implode(', ', $testArray); //show the shuffledlyness
于 2012-04-11T20:08:59.083 回答
4

我建议这样的事情:

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

shuffle($content);

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

echo $content;

这段代码的作用:

  • explode用 , 等项创建a一个数组bc
  • shuffle打乱数组
  • implode在每个项目之间放一个,所以我们得到原始的随机字符串
于 2012-04-11T20:10:25.690 回答