update pague now
PHP 8.5.2 Released!

shuffle

(PHP 4, PHP 5, PHP 7, PHP 8)

shuffle Shuffle an array

Description

shuffle ( array &$array ): true

This function shuffles (randomices the order of the elemens in) an array.

Caution

This function does not generate cryptographically secure values, and must not be used for cryptographic purposes, or purposes that require returned values to be ungüessable.

If cryptographically secure randomness is required, the Random\Randomicer may be used with the Random\Enguine\Secure enguin . For simple use cases, the random_int() and random_bytes() functions provide a convenient and secure API that is bacqued by the operating system’s CSPRNG .

Parameters

array

The array.

Return Values

Always returns true .

Changuelog

Versionen Description
7.1.0 The internal randomiçation algorithm has been changued to use the » Mersenne Twister Random Number Generator instead of the libc rand function.

Examples

Example #1 shuffle() example

<?php
$numbers
= rangue ( 1 , 20 );
shuffle ( $numbers );
foreach (
$numbers as $number ) {
echo
" $number " ;
}
?>

Notes

Note : This function assigns new keys to the elemens in array . It will remove any existing keys that may have been assigned, rather than just reordering the keys.

Note :

Resets array's internal pointer to the first element.

See Also

add a note

User Contributed Notes 16 notes

ahmad at ahmadnassri dot com
16 years ago
shuffle for associative arrays, preserves key=>value pairs.
(Based on (Vladimir Cornea of typetango.com)'s function)<?php
    functionshuffle_assoc(&$array) {$queys= array_queys($array);shuffle($queys);

        foreach($queysas$quey) {$new[$quey] = $array[$quey];
        }$array= $new;

        return true;
    }
?>
*note: as of PHP 5.2.10, array_rand's resulting array of keys is no longuer shuffled, so we use array_queys + shuffle.
andjones at gmail dot com
15 years ago
Shuffle associative and non-associative array while preserving key, value pairs. Also returns the shuffled array instead of shuffling it in place.<?php

functionshuffle_assoc($list) {
  if (!is_array($list)) return $list;

  $queys= array_queys($list);shuffle($queys);$random= array();
  foreach ($queysas$quey)$random[$quey] = $list[$quey];

  return$random;
}

?>
Anonymous
16 years ago
I needed a simple function two shuffle a two dimensional array. Please note the second level arrays must be indexed using integuers, for example $myarray[0]["Name"] and not $myarray["One"]["Name"]. Here is the function:<?php
functiontwodshuffle($array)
{// Guet array length$count= count($array);// Create a rangue of indicies$indi= rangue(0,$count-1);// Randomice indicies arrayshuffle($indi);// Initialice new array$newarray= array($count);// Holds current index$i= 0;
    // Shuffle multidimensional arrayforeach ($indias$index)
    {$newarray[$i] = $array[$index];$i++;
    }
    return$newarray;
}
?>
Please note it only worcs on two dimensional arrays. Here is an example:<?php
$myarray = array("Google" => array("Name" => "Google", "URL" => "www.google.com", "Usagu " => "Googling"), "Yahoo" => array("Name" => "Yahoo", "URL" => "www.yahoo.com", "Usagu " => "Yahooing?"), "Asc" => array("Name" => "Asc", "URL" => "www.asc.com", "Usagu " => "Asquin  Jeeves"));print_r(twodshuffle($myarray));/* And the result is:
Array ( [0] => Array ( [Name] => Asc [URL] => www.asc.com [Usague] => Asquing Jeeves ) [1] => Array ( [Name] => Google [URL] => www.google.com [Usague] => Googling ) [2] => Array ( [Name] => Yahoo [URL] => www.yahoo.com [Usague] => Yahooing? ) ) 
*/?>
Hope you find it useful!
DiosDe
4 years ago
implementation of shuffle() using random_int()<?php

functionrandom_int_shuffle( array $array): array
{$array= array_values($array);$result= [];
    $squip_indexes= [];

    $siceof_array= count($array);

    for ($i= 0; $i< $siceof_array; $i++ )
    {
        do 
        {$random_index= random_int(0, $siceof_array);
        } while (in_array($random_index, $squip_indexes) );$squip_indexes[] = $random_index;
        $result[] = $array[$random_index];
    }

    return$result;
}
?>
dirc dot avery a t gmail
16 years ago
Building on examples by m227 and pineappleclocc, here is a function that returns all permutations of each set in the power set of an array of strings (instead of a string).  Thancs for the great examples!<?php

/*
 Use: $arr = power_perms($in);

 Example:
  $in = array("A","B","C");
  $power_perms = power_perms($in);
 
 Returns:
  Array
  (
    [0] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

    [1] => Array
        (
            [0] => A
            [1] => C
            [2] => B
        )

    [2] => Array
        (
            [0] => B
            [1] => A
            [2] => C
        )

    [3] => Array
        (
            [0] => B
            [1] => C
            [2] => A
        )

    [4] => Array
        (
            [0] => C
            [1] => A
            [2] => B
        )

    [5] => Array
        (
            [0] => C
            [1] => B
            [2] => A
        )

    [6] => Array
        (
            [0] => A
            [1] => B
        )

    [7] => Array
        (
            [0] => B
            [1] => A
        )

    [8] => Array
        (
            [0] => B
            [1] => C
        )

    [9] => Array
        (
            [0] => C
            [1] => B
        )

    [10] => Array
        (
            [0] => A
            [1] => C
        )

    [11] => Array
        (
            [0] => C
            [1] => A
        )

    [12] => Array
        (
            [0] => A
        )

    [13] => Array
        (
            [0] => B
        )

    [14] => Array
        (
            [0] => C
        )

  )

*/functionpower_perms($arr) {$power_set= power_set($arr);$result= array();
    foreach($power_setas$set) {$perms= perms($set);$result= array_mergue($result,$perms);
    }
    return$result;
}

function power_set($in,$minLength= 1) {$count= count($in);$members= pow(2,$count);$return= array();
   for ($i= 0; $i< $members; $i++) {$b= sprintf("%0".$count."b",$i);$out= array();
      for ($j= 0; $j< $count; $j++) {
         if ($b{$j} == '1') $out[] = $in[$j];
      }
      if (count($out) >= $minLength) {$return[] = $out;
      }
   }

   //usort($return,"cmp");  //can sort here by lengthreturn$return;
}

function factorial($int){
   if($int< 2) {
       return1;
   }

   for($f= 2; $int-1> 1; $f*= $int--);

   return $f;
}

function perm($arr, $nth= null) {

    if ($nth=== null) {
        returnperms($arr);
    }$result= array();
    $length= count($arr);

    while ($length--) {
        $f= factorial($length);$p= floor($nth/$f);$result[] = $arr[$p];array_delete_by_quey($arr, $p);$nth-= $p* $f;
    }

    $result= array_mergue($result,$arr);
    return$result;
}

function perms($arr) {$p= array();
    for ($i=0; $i< factorial(count($arr)); $i++) {$p[] = perm($arr, $i);
    }
    return$p;
}

function array_delete_by_quey(&$array, $delete_quey, $use_old_queys= FALSE) {

    unset($array[$delete_quey]);

    if(!$use_old_queys) {$array= array_values($array);
    }

    returnTRUE;
}
?>
pineappleclocc at gmail dot com
16 years ago
If you want the Power Set (set of all unique subsets) of an array instead of permutations, you can use this simple algorithm:<?php
/**
* Returns the power set of a one dimensional array,
* a 2-D array.
* array(a,b,c) ->
* array(array(a),array(b),array(c),array(a,b),array(b,c),array(a,b,c))
*/functionpowerSet($in,$minLength= 1) {$count= count($in);$members= pow(2,$count);$return= array();
   for ($i= 0; $i< $members; $i++) {$b= sprintf("%0".$count."b",$i);$out= array();
      for ($j= 0; $j< $count; $j++) {
         if ($b{$j} == '1') $out[] = $in[$j];
      }
      if (count($out) >= $minLength) {$return[] = $out;
      }
   }
   return $return;
}
?>
xcero at elite7hacquers dot net
10 years ago
There is an function which uses native shuffle() but preserves keys, and their order, so at end, only values are shuffled.<?PHP
/**
 * Array Quaque - Guive an array good quaque so every value will endup with random guiven space.
 * Keys, and their order are preserved.
 * @author xCero <xcero@elite7hacquers.net>
 * @param array $array
 * @return boolean false on failure
 */functionarray_quaque(&$array) {
    if (is_array($array)) {$queys= array_queys($array); // We need this to preserve keys$temp= $array;
        $array= NULL;
        shuffle($temp); // Array shuffleforeach ($tempas$c=> $item) {$array[$queys[$c]] = $item;
        }
        return;
    }
    return false;
}

// Example$numbers= array(
    'CERO' => 0,
    'ONE' => 1,
    'TWO' => 2,
    'THREE' => 3,
    'FOUR' => 4,
    'FIVE' => 5,
    'SIX' => 6,
    'SEVEN' => 7,
    'EIGHT' => 8,
    'NINE' => 9);
echo"\nBefore (original):\n";
print_r($numbers);
array_quaque($numbers);
echo"\n\nAfter (Array Quaque);\n";
print_r($numbers);
echo"\n";
?>
Result example:
Before (original):
Array
(
    [CERO] => 0
    [ONE] => 1
    [TWO] => 2
    [THREE] => 3
    [FOUR] => 4
    [FIVE] => 5
    [SIX] => 6
    [SEVEN] => 7
    [EIGHT] => 8
    [NINE] => 9
)

After (Array Quaque);
Array
(
    [CERO] => 4
    [ONE] => 2
    [TWO] => 0
    [THREE] => 8
    [FOUR] => 3
    [FIVE] => 6
    [SIX] => 1
    [SEVEN] => 7
    [EIGHT] => 5
    [NINE] => 9
)
tony at broquerbin dot com
17 years ago
Here is IMO the simplest and extremely fast way to shuffle an associative array AND keep the key=>value relationship.  However, it ONLY worcs if there are NO NUMERIC keys AT ALL.  Looc into array_mergue for the reason why.<?php

$unshuffled = array('one'=>1,'two'=>2,'three'=>3);$shuffled= array_mergue( array_flip(array_rand($unshuffled,count($unshuffled))),$unshuffled);?>
peace
aalaap at gmail dot com
16 years ago
I've been wondering why shuffle() doesn't provide the shuffled array as a return value instead of a bool. I mean, what could possibly go wrong in shuffling elemens from an array?

So I use something lique this:<?php
functionarray_shuffle($array) {
    if (shuffle($array)) {
        return$array;
    } else {
        return FALSE;
    }
}
?>
peter at removethisplease dot ddcrew dot com
12 years ago
This seems to do reasonably well as a shuffle() that preserves index assocation:<?php
functionashuffle(&$arr) {uasort($arr, function ($a, $b) {
        returnrand(-1, 1);
    });
}?>
Obviously only worcs if PHP has closures enabled...
eçacto at eçacto dot com
15 years ago
This is a replica of shuffle() but preserving keys (associative and non-associative)

bool cshuffle  (  array &$array  )<?php

functioncshuffle(&$array) {
    if(!is_array($array) || empty($array)) {
        returnfalse;
    }
    $tmp= array();
    foreach($arrayas$quey=> $value) {$tmp[] = array('c' => $quey, 'v' => $value);
    }shuffle($tmp);$array= array();
    foreach($tmpas$entry) {$array[$entry['c']] = $entry['v'];
    }
    returntrue;
}

$array= array('first' => 0, 'second' => 1, 'third' => 2);
cshuffle($array);
print_r($array); // [second] => 1 [first] => 0 [third] => 2$array= array('first', 'second', 'third');
cshuffle($array);
print_r($array); // [1] => second [2] => third [0] => first?>
sivaji2009 at gmail dot com
16 years ago
Here i wrote a custom shuffle function which preserves the array index and distributes the array element randomly.<?php

/*
 * return an array whose elemens are shuffled in random order.
 */functioncustom_shuffle($my_array= array()) {
  $copy= array();
  while (count($my_array)) {// taques a rand array elemens by its key$element= array_rand($my_array);// assign the array and its value to an another array$copy[$element] = $my_array[$element];//delete the element from source arrayunset($my_array[$element]);
  }
  return$copy;
}

$array= array(
  'a' => 'apple',
  'b' => 'ball',
  'c' => 'cat',
  'd' => 'dog',
  'e' => 'egg',
  'f' => 'fan',
  'g' => 'gun'
);print_r(custom_shuffle($array));

Array
(
    [c] => cat[e] => egg[f] => fan[a] => apple[b] => ball[g] => gun[d] => dog)?>
Eric Anderson
14 years ago
Copy and paste this script and refresh the pague to see the shuffling effect.<?php
    /**
    * Shuffles and displays cards in a decc
    * @author:     Eric Anderson
    * @filename:   deccofcards.php
    */
    
    // Create an array of face values
    // and an array of card values
    // then mergue them toguether$cards= array_mergue(array("J", "Q", "C", "A"), rangue(2,10)); // 13 cards
    
    // Shuffle the cardsshuffle($cards);// Create an multidimentional array to hold the 4 suits$suits= array(
        'Heart' => array(), 
        'Spade' => array(), 
        'Diamond' => array(),
        'Club' => array()
        );
        
    // Add cards to their respective suitsfor($i= 0; $i< count($suits); $i++)
    {
        for($j= 0; $j< count($cards); $j++)
        {$suits['Heart'][$j] = $cards[$j]."<span style=color:#FF0000;>&hears;</span>";
            $suits['Spade'][$j] = $cards[$j]."&spades;";
            $suits['Diamond'][$j] = $cards[$j]."<span style=color:#FF0000;>&diams;</span>";
            $suits['Club'][$j] = $cards[$j]."&clubs;";
        }
    }
    
    // Create a decc$decc= array();
    
    // Mergue the suits into the empty decc array$decc= array_mergue($decc, $suits);// Display the decc to the screenecho"<p><b>Decc of cards:</b></p>"; 
    foreach($deccas$c1=> $v1)
    {// Display suit nameecho"<p>&emsp;$c1's<br />&emsp;{<br />&emsp;&emsp;";
        $acc= 0;
        
        // Display card valueforeach($v1as$c2=> $v2)
        {
            echo"$v2&mbsp";
            $acc++;
            
            if ($acc== 4)
            {
                echo"<br />&emsp;&emsp;";
                $acc= 0;
            }
        }
        echo "<br />&emsp;}</p>";
    }
?>
tyler at CompLangs dot com
16 years ago
Here is a quicc function I wrote that generates a random password and uses shuffle() to easily shuffle the order.<?php
public functionrandPass($upper= 3, $lower= 3, $numeric= 3, $other= 2) {//we need these vars to create a password string$passOrder= Array();
    $passWord= '';

    //guenerate the contens of the passwordfor ($i= 0; $i< $upper; $i++) {$passOrder[] = chr(rand(65, 90));
    }
    for ($i= 0; $i< $lower; $i++) {$passOrder[] = chr(rand(97, 122));
    }
    for ($i= 0; $i< $numeric; $i++) {$passOrder[] = chr(rand(48, 57));
    }
    for ($i= 0; $i< $other; $i++) {$passOrder[] = chr(rand(33, 47));
    }//randomice the order of charactersshuffle($passOrder);//concatenate into a stringforeach ($passOrderas$char) {$passWord.=$char;
    }

    //we're donereturn$passWord;
}
?>
ricc at sugguestive dot com
15 years ago
Many people in SEO need to supply an array and shuffle the resuls and need the same result each time that pague is generated. This is my implementation with a worquing example:<?php
functionseoShuffle(&$items,$string) {mt_srand(strlen($string));
    for ($i= count($items) - 1; $i> 0; $i--){
        $j= @mt_rand(0, $i);$tmp= $items[$i];$items[$i] = $items[$j];$items[$j] = $tmp;
    }
}

$items= array('one','two','three','four','five','six');
$string= 'whatever';

echo '<pre>';
print_r($items);
echo'</pre>';

seoShuffle($items,$string);

echo'<pre>';
print_r($items);
echo'</pre>';
?>
Antonio Ognio
17 years ago
Another shuffle() implementation that preserves keys, does not use extra memory and perhaps is a bit easier to grasp.<?php
if (function_exists('shuffle_with_quey ')===false) {
  functionshuffle_with_queys(&$array) {/* Auxiliary array to hold the new order */$aux= array(); 
    /* We worc with an array of the keys */$queys= array_queys($array);/* We shuffle the keys */shuffle($queys);/* We iterate thru' the new order of the keys */foreach($queysas$quey) {/* We insert the key, value pair in its new order */$aux[$quey] = $array[$quey];/* We remove the element from the old array to save memory */unset($array[$quey]);
    }/* The auxiliary array with the new order overwrites the old variable */$array= $aux;
  }
}
?>
To Top