PHP | shuffle() Function

The shuffle() Function is a builtin function in PHP and is used to shuffle or randomize the order of the elements in an array. This function assigns new keys for the elements in the array. It will also remove any existing keys, rather than just reordering the keys and assigns numeric keys starting from zero.

Syntax:

boolean shuffle($array)

Parameter: This function accepts a single parameter $array. It specifies the array we want to shuffle.



Return Value: This function returns a boolean value i.e., either True or False. It returns TRUE on success and FALSE on failure.

Note: This function will work for PHP version 4+.

Examples:

Input:- array("a"=>"Ram", 
              "b"=>"Shita", 
              "c"=>"Geeta", 
              "d"=>"geeksforgeeks" )
Output:- array( [0] => Geeta,
                [1] => Shita,
                [2] => Ram,
                [3] => geeksforgeeks )
Explanation: Here as we can see that input contain elemets 
             in a order but in output order become suffled.

Below programs illustrates working of shuffle() in PHP:

  1. When the input array is an associative array, then the shuffle() function will randomize the order of elements as well as assigns new keys to the elements starting from zero (0).
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // input array contain some elements which
    // need to be shuffled.
    $a = array
         (  
            "a"=>"Ram"
            "b"=>"Shita"
            "c"=>"Geeta"
            "d"=>"geeksforgeeks"
         );
      
    shuffle($a);
    print_r($a);
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [0] => geeksforgeeks
        [1] => Shita
        [2] => Ram
        [3] => Geeta
    )
    
  2. When the input array is not associative then the shuffle() function will randmize the order and covert the array to an associative array with keys starting from zero (0).

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // input array contain some elements
    // which need to be shuffled.
    $a = array
         (
            "ram",
            "geeta",
            "blue",
            "red",
            "shyam"
         );
      
    shuffle($a);
    print_r($a);
      
    ?>

    chevron_right

    
    

    Output:

    Array
    (
        [0] => red
        [1] => geeta
        [2] => ram
        [3] => shyam
        [4] => blue
    )
    

Reference:
http://php.net/manual/en/function.shuffle.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.