PHP shuffle() Function
In this chapter you will learn:
- Definition for PHP shuffle() Function
- Syntax for PHP shuffle() Function
- Parameter for PHP shuffle() Function
- Return for PHP shuffle() Function
- Example for PHP shuffle() Function
- Example - Randomize the order of the elements in the array
- Example - Randomize the order of the elements in the associate array
Definition
The shuffle()
function randomizes the position of the array elements.
Syntax
PHP shuffle() Function has the following syntax.
bool shuffle ( array &arr )
Parameter
Parameter | Is Required | Description |
---|---|---|
arr | Required. | 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 j a va2s. co 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/*j a va 2 s.co m*/
$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//j a v a 2 s. 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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP sizeof() Function
- Syntax for PHP sizeof() Function
- Parameter for PHP sizeof() Function
- Example for PHP sizeof() Function
- Example - Count the array recursively
Home » PHP Tutorial » PHP Array Functions