如何在 PHP 中将数字更改为随机位置


How to change numbers to random positions in PHP

我将如何在php中将以下数字更改为随机位置?

我需要分解数字吗?

40,52,78,81,25,83,37,77

谢谢

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

http://ideone.com/sh2uH

所以你想洗牌数组顺序吗? 使用 PHP 的随机函数。

http://php.net/manual/en/function.shuffle.php

编辑:没有意识到你的数字在一个字符串中。另一个答案总结了它。

假设数字在一个字符串中:

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

尝试这样的事情。

$string = "40,52,78,81,25,83,37,77";
$numbers = explode(",", $string);
shuffle($numbers);
print_r($numbers);

explode将字符串分解为一个数组,按,分隔条目

shuffle 将通过引用对数组进行操作,并按随机顺序放置它们