Latest web development tutorials

PHP shuffle () function

PHP Array Reference Complete PHP Array Reference

Examples

The elements in the array are rearranged in random order:

<?php
$my_array = array("red","green","blue","yellow","purple");

shuffle($my_array);
print_r($my_array);
?>

Running instance »

Definition and Usage

shuffle () function to rearrange the elements of the array in random order.

This function allocates a new name for the key elements in the array, existing keys will be deleted (see Example 1 below).


grammar

shuffle( array )

参数 描述
array 必需。规定要使用的数组。

technical details

return value: If successful it returns TRUE, on failure returns FALSE.
PHP version: 4+
Update log: As of PHP 4.2.0, the random number generator is automatically seeded.


More examples

Example 1

The elements in the array are rearranged in random order:

<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");

shuffle($my_array);
print_r($my_array);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference