Reputation: 6389
I'm using shuffle()
to randomly generate items on my site like so:
shuffle($items);
$shirts = array();
foreach ($items as $key => &$row) {
$shirts[$row['Id']] = $row['shirts'];
}
The code goes further, but basically it's running a foreach
and displays 12 results. However, shuffle()
seems to only return the first 12 items in the array and shuffle them. The array may contain dozens of items, and I want to shuffle through the entire array. What am I doing wrong?
Upvotes: 0
Views: 170
Reputation: 29
I just wrote this function to shuffle an array. It return the array shuffled so you can still keep the original array:
`function lowellshuffle($unshuff) {
$co = count($unshuff);
$m=0;
for ($i=0;$i<$co;$i++){
$p = rand(0,count($unshuff)-1);
$shuffled[$i] = $unshuff[$p];
for ($j=0;$j<count($unshuff);$j++){
if ($unshuff[$j] !== $shuffled[$i]){
$nq[$j- $m] = $unshuff[$j];
}
else {$m++;}
}
unset($unshuff);
$unshuff = $nq;
unset($nq);
$m=0;
}
return $shuffled;
}`
Upvotes: 0
Reputation: 25228
We need to see more code. As of right now according to the code, it should display every result (not just 12). This must mean that you're cutting the array down to 12 before you even shuffle it.
Upvotes: 1