3

what is the php function to randomize the associative array while keeping key/values pairs. I don't mean to just randomly pick out a key value pair, but actually changing the array (similar to the uasort function, but not in order).

TIA

example:

original array
(
    [a] => 4
    [b] => 8
    [c] => -1
    [d] => -9
    [e] => 2
    [f] => 5
    [g] => 3
    [h] => -4
)

random ordered array
(

[d] => -9
[a] => 4
[b] => 8
[c] => -1
[h] => -4   
[e] => 2
[g] => 3
[h] => -4
[f] => 5
)

Edit Comparison between 2 solutions.

$start = microtime(true);
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$shuffleKeys = array_keys($array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
    $newArray[$key] = $array[$key];
}
print_r ($newArray);
$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.<br>";

$start = microtime(true);
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$keys = array_keys( $array );
   shuffle( $keys );
   print_r(array_merge( array_flip( $keys ) , $array ));


$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.<br>";

Array ( [h] => -4 [e] => 2 [b] => 8 [d] => -9 [a] => 4 [c] => -1 [f] => 5 [g] => 3 ) array values took 3.0994415283203E-5 seconds.

Array ( [e] => 2 [a] => 4 [d] => -9 [c] => -1 [g] => 3 [f] => 5 [b] => 8 [h] => -4 ) array values took 4.2915344238281E-5 seconds.

Jamex
  • 1,164
  • 5
  • 22
  • 34
  • My first thought was to use uasort() with a callback that generates a random number in the range -1 - 1. However, I've no idea what would happen if you used a non-deterministic callback with uasort. It may have unpleasant side-effects. – GordonM Sep 18 '11 at 19:00
  • Thanks Gordon, it is a great idea to test. – Jamex Sep 18 '11 at 19:23

2 Answers2

7

You could use shuffle() on array_keys, then loop around your array adding them to the list in the new order.

E.g.

$shuffleKeys = array_keys($array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
    $newArray[$key] = $array[$key];
}
Jonnix
  • 4,121
  • 1
  • 30
  • 31
  • Edited to actually be valid -_- – Jonnix Sep 18 '11 at 19:12
  • Thanks Jon, great code, it will be interesting see if this code is faster than str's. – Jamex Sep 18 '11 at 19:34
  • There is an extra 's' (or missing 's') in "$shuffleKey". Otherwise, the function works. Your code is faster than the code that str referenced. See the results in the original post. – Jamex Sep 18 '11 at 19:53
  • @Jamex: The processing time of a code snippet is NOT deterministic. So you have to repeat the code and take the average time to compare their performance. – str Sep 19 '11 at 06:35
5

A comment on shuffle() might do the trick: http://ch2.php.net/manual/en/function.shuffle.php#104430

<?php
function shuffle_assoc( $array )
{
   $keys = array_keys( $array );
   shuffle( $keys );
   return array_merge( array_flip( $keys ) , $array );
}
?>
str
  • 42,689
  • 17
  • 109
  • 127