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:

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: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises