Reputation: 6189
How would I go about changing the following numbers around to a random positions in php?
Do I need to explode the numbers?
40,52,78,81,25,83,37,77
Thanks
Upvotes: 0
Views: 213
Reputation: 7999
Try something like this.
$string = "40,52,78,81,25,83,37,77";
$numbers = explode(",", $string);
shuffle($numbers);
print_r($numbers);
explode
breaks the string out into an array separating entries by ,
shuffle
will operate on the array by reference and put them in random order
Upvotes: 0
Reputation: 12776
Assuming the numbers are in a string:
$numbers = '40,52,78,81,25,83,37,77';
$numbers = explode(',',$numbers);
shuffle($numbers);
$numbers = implode(',',$numbers);
Upvotes: 0
Reputation: 254926
$arr = explode(',', '40,52,78,81,25,83,37,77');
shuffle($arr);
echo implode(',', $arr);
Upvotes: 5
Reputation: 6043
So you want to shuffle the array order? Use PHP's shuffle function.
http://php.net/manual/en/function.shuffle.php
EDIT: Didn't realise your numbers were in a string. The other answer sums it up.
Upvotes: 1