How to shuffle Array and Randomize the order of the elements in the array in PHP

shuffle() doesn't provide the shuffled array as a return value. Its simply gives bool value instead of shuffled array. If you want to make shuffle(), which gives shuffling elements from an array then use below code.

<?php
     
function array_shuffle($array) {
      if (shuffle($array)) {
           return $array;
      } else {
           return FALSE;
      }
}
$my_array = array("red","green","blue","yellow","purple");
echo "<pre>";
print_r(array_shuffle($my_array));
echo "</pre>";
?>

Post a Comment

0 Comments