The shuffle()
function randomizes the position of the array elements.
PHP shuffle() Function has the following syntax.
bool shuffle ( array &arr )
Parameter | Is Required | Description |
---|---|---|
arr | Required. | Array to use |
It takes its parameter by reference and return either true or false, depending on whether it successfully randomized the array.
<?PHP
$value = array("A", "B", "C", "java2s.com");
shuffle($value);
print_r($value);
?>
The code above generates the following result.
Randomize the order of the elements in the array:
<?php
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
print_r($my_array);
?>
The code above generates the following result.
Randomize the order of the elements in the array:
<?php
$my_array = array("a"=>"A","b"=>"B","c"=>"C","d"=>"D","e"=>"java2s.com");
shuffle($my_array);
print_r($my_array);
?>
The code above generates the following result.