Home >>PHP Array Functions >PHP shuffle() Function

PHP shuffle() Function

PHP shuffle() Function

PHP shuffle() function is used to shuffle or randomize the order of the elements in the given input array. It assigns new keys for the elements present in the array. It accepts only a single parameter $array that is the given input array. It returns a boolean value TRUE on success and FALSE on failure.

Syntax:

shuffle($array);

Parameter Values

Parameter Description
array This is a required parameter. It defines the array to use.

Here is an example of shuffle() function in PHP:

<html>
<body>
<pre>
<?php
$arr = array("0","1","2","3","4","5","6","7","8","9");
shuffle($arr);
print_r($arr);
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => 7
    [1] => 4
    [2] => 5
    [3] => 0
    [4] => 9
    [5] => 6
    [6] => 1
    [7] => 8
    [8] => 3
    [9] => 2
)

Example 2:

<html>
<body>
<pre>
<?php
$arr = array("a"=>"0","b"=>"1","c"=>"2","d"=>"3","e"=>"4","f"=>"5","g"=>"6","h"=>"7","i"=>"8","j"=>"9");
shuffle($arr);
print_r($arr);
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => 2
    [1] => 6
    [2] => 4
    [3] => 5
    [4] => 1
    [5] => 3
    [6] => 8
    [7] => 0
    [8] => 7
    [9] => 9
)

No Sidebar ads