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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/function-reference/shuffle.php