Page 1 of 1
shuffle?
Posted: Thu Jan 19, 2006 8:33 pm
by jonathant
Is there a function that will take a numeric array and just randomize the items within it? I was trying to use shuffle() but it was returning a boolean, and not the rearranged array itself. PHP5 probably has a function that does this, but I'm not having any luck finding it.
Code: Select all
$my_array=(1, 2, 3, 4);
$new_array=randomize_function($my_array);
// now, $new_array should equal array(3, 1, 4, 2);
Re: shuffle?
Posted: Thu Jan 19, 2006 8:47 pm
by Christopher
jonathant wrote:I was trying to use shuffle() but it was returning a boolean, and not the rearranged array itself.
It shuffles the array you pass it.
Code: Select all
$my_array=(1, 2, 3, 4);
$new_array=$my_array;
shuffle($new_array);
Posted: Thu Jan 19, 2006 8:55 pm
by jonathant
Here is what I'm trying to do. $hompage_list_array contains item ids in a numeric array.
Code: Select all
$list_shuffled=shuffle($homepage_list_array); // shuffle the order, so the displayed items will be random
while (count($list_shuffled) <= // only take the first 8 because that is all the homepage template can fit
{
array_pop($list_shuffled);
}
return $list_shuffled;
But, I'm getting this error: Warning: array_pop() [function.array-pop]: The argument should be an array in C:\Program Files\xampp\htdocs\dev\hubuy\includes\datasearch.php5 on line 36
Posted: Thu Jan 19, 2006 9:11 pm
by John Cartwright
Might help to take a look at
the manual 
Re: shuffle?
Posted: Thu Jan 19, 2006 9:53 pm
by Christopher
Code: Select all
$my_array=(1, 2, 3, 4, 6, 7, ;
$new_array=$my_array;
shuffle($new_array);
$newer_array = array_slice($new_array, 0, ;
Posted: Fri Jan 20, 2006 1:51 am
by s.dot
don't store the shuffled array in a variable
Code: Select all
// this is correct
$array = array(1,2,3,4,5);
shuffle($array);
print_r($array);
// this is incorrect
$array = array(1,2,3,4,5);
$shuffled = shuffle($array);
print_r($shuffled);