Forum und email

shuffle

(PHP 4, PHP 5)

shuffle — Shuffle an array

Description

bool shuffle ( array &$array )

This function shuffles (randomizes the order of the elements in) an array.

Note: 本函式為 array 中的元素賦予新的鍵名。這將刪除原有的鍵名而不僅是重新排序。

Example#1 shuffle() example

<?php
$numbers 
range(120);
srand((float)microtime() * 1000000);
shuffle($numbers);
foreach (
$numbers as $number) {
    echo 
"$number ";
}
?>

Note: 自 PHP 4.2.0 起,不再需要用 srand()mt_srand() 函式給亂數產生器種子,現在將會自動完成。

See also arsort(), asort(), ksort(), rsort(), sort(), and usort().