2012-04-11 86 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