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/*from   w ww.  ja v a2  s  . c o m*/
$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//from  w w w . j  a  v a 2 s. com
$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//  www  .  j a  v a  2s  .c o m
$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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions