w3resource

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.

Example:

<?php
$country_list = array(1 => "India", 2 => "USA", 3 => "Uk");
shuffle($country_list);
print_r($country_list);
?>

Output:

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

Pictorial Presentation:

php function reference: shuffle() function
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.

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: rsort
Next: sizeof



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);