PHP’s shuffle() destroys your array keys

Note: This function assigns new keys for the elements in array . It will remove any existing keys you may have assigned, rather than just reordering the keys.

My favorite. Here is the painfully artless workaround:

$data = array(1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd');
$out = array();
$keys = array_keys($data);
shuffle($keys);
foreach ($keys as $key) {
    $out[$key] = $data[$key];
}

Tastes like burning.

Leave a Reply