What is the array shuffle() method in PHP?
The shuffle method can be used to randomly shuffle the position of the elements of the array.
Syntax
shuffle(array &$array): bool
This method returns true if the shuffling is done successfully. Otherwise, it returns false.
This method will not maintain the keys of the elements. Instead, new numeric keys will be assigned to the elements.
Example
<?php$numbers = ['one' => 1,'two' => 2,3,4,'five' => 5];echo "The Array is \n";print_r($numbers);echo "Shuffling numbers :". shuffle($numbers). "\n";echo "Array after shuffling \n";print_r($numbers);?>
In the code above:
-
We created a
numbersarray and printed it. -
We used the
shufflemethod to shuffle the position of the elements of the array. -
We printed the shuffled array. In the shuffled array, the old keys are not maintained. Instead, new numeric keys are assigned.