Rickstar
Rickstar

Reputation: 6189

How to change numbers to random positions in PHP

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

Answers (4)

jcbwlkr
jcbwlkr

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

lafor
lafor

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

zerkms
zerkms

Reputation: 254926

$arr = explode(',', '40,52,78,81,25,83,37,77');
shuffle($arr);
echo implode(',', $arr);

http://ideone.com/sh2uH

Upvotes: 5

n00dle
n00dle

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

Related Questions