PHP shuffle

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: shuffle() function

PHP: Shuffle an array

The shuffle() function is used to randomize the order of the elements in the array. The function assigns new keys to the elements in array.

Note: The function uses a pseudo-random number generator that is not suitable for cryptographic purposes.

Version

(PHP 4 and above)

Syntax

shuffle(array_name)

Parameter

Name

Description

Required /
Optional

Type

array_name

The input array.

Required

Array

Return value

TRUE on success or FALSE on failure.

Value Type : Boolean.

Pictorial Presentation :

php array shuffle() function

Example :

  1. $country_list = array(1 => "India", 2 => "USA", 3 => "Uk");  

  2. shuffle($country_list);  

  3. print_r($country_list);  

  4. ?>  

Output :

Array ( [0] => Uk [1] => USA [2] => India )

Note : Since shuffle() function randomize the order of the elements in an array every time you run it, your output may vary from the Pictorial as well the output shown under Output of the example in this tutorial.

Practice here online :