How to slice array then shuffle it
<p>I have an function that creates an array of words from a string, counts how often each word occurs and then selects the top 21 words. </p>

<p>Trouble I'm having is I then need to shuffle those 21 words. If I try shuffle() my foreach loop will output the number of occurences rather than the word itself.</p>

<p>Can someone show me how to do this? Here is my existing function:</p>

<pre><code>$rawstring = implode(" ", $testimonials);

$rawstring = filterBadWords($rawstring);

// get the word=&gt;count array
$words = array_count_values(str_word_count($rawstring, 1));

// sort on the value (word count) in descending order
arsort($words);


// get the top frequent words
$top10words = array_slice($words, 0, 21);
shuffle($top10words);

foreach($top10words as $word =&gt; $value) {
    $class = getClass($value);
    echo "&lt;a href=\"#\" id=\"" . $word . "\" class=\"" . $class . "\"&gt;" . $word . "&lt;/a&gt;";
}
</code></pre>
