PHP Tutorial - PHP shuffle() Function






Definition

The shuffle() function randomizes the position of the array elements.

Syntax

PHP shuffle() Function has the following syntax.

bool shuffle ( array &arr )

Parameter

ParameterIs RequiredDescription
arrRequired.Array to use

Return

It takes its parameter by reference and return either true or false, depending on whether it successfully randomized the array.





Example 1


<?PHP
$value = array("A", "B", "C", "java2s.com");
shuffle($value);
print_r($value);
?>

The code above generates the following result.

Example 2

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.





Example 3

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.