2012-04-11 79 views
1

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

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

我想它随机最好的办法,例如

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

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

任何解决方案?

+1

您是否尝试过自己什么? – 2012-04-11 20:09:02

+0

http://us2.php.net/manual/en/function.rand.php – 2012-04-11 20:09:04

+0

@Paul比'rand()'有更简单的方法。数组到'shuffle()'是我开始的地方... – 2012-04-11 20:10:34

回答

4

我建议是这样的:

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

shuffle($content); 

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

echo $content; 

这段代码的含义:

  • explode使得与像abc ECT项的数组。
  • shuffle洗牌阵列
  • implode把各项目之间的,所以我们获得了原始的随机字符串返回
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