PHP shuffle() Function Explained
The shuffle function is an array function in PHP that randomly rearranges the elements in an array. It modifies the original array and returns a boolean value indicating whether the array was successfully shuffled.
The syntax for using this function is as follows:
shuffle(array &$array): bool
Parameter description:
- $array: Array required. An array that needs to be shuffled. Note: Prefixing the parameter with the & symbol indicates that an array reference is being passed, allowing the function to directly modify the original array.
例子:The professor explained the concept in great detail.
$myArray = array("apple", "banana", "cherry", "date");
shuffle($myArray);
print_r($myArray);
The possible output could be:
Array
(
[0] => cherry
[1] => date
[2] => banana
[3] => apple
)
In the example above, the shuffle function randomly rearranged the elements of the array $myArray and displayed the result.