shuffle an array in php

To shuffle an array in PHP, we can use the shuffle() function. This function randomizes the order of the elements in an array. Here's an example:

main.php
$myArray = array('apple', 'banana', 'cherry', 'date', 'elderberry');
shuffle($myArray);
print_r($myArray);
107 chars
4 lines

This code will output something like:

main.php
Array
(
    [0] => cherry
    [1] => apple
    [2] => date
    [3] => banana
    [4] => elderberry
)
101 chars
9 lines

As you can see, the shuffle() function has randomized the order of the elements in the array. Now, whenever you run the code, you'll get a different order of elements. This is a simple and quick way to randomize the order of elements in an array.

gistlibby LogSnag