Randomize the order of the elements in the array in PHP

Description

The following code shows how to randomize the order of the elements in the array.

Example


  /*  w  w w . j a  va 2  s  .c om*/
<?php
    $my_array = array("red","green","blue","yellow","purple");
   
    shuffle($my_array);
    print_r($my_array);

    $my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");
    
    shuffle($my_array);
    print_r($my_array);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone