我认为您的问题可以通过这种简单的方式解决:您需要将您的 IDs 数组重复到所需的数量,然后对其进行随机播放。可能有一个“尾巴”(如果total count
/count of IDs
不是整数) - 为了做出更好的随机性,我建议从原始数组中检索随机 ID。这是一个示例:
$rgIds = [5, 72, 10, 93];
$iCount = 30;
$iLoop = (int)($iCount/count($rgIds)); //count of repeats
$rgResult = array_intersect_key( //'tail' from random Ids
$rgIds,
array_flip(array_rand($rgIds, $iCount%count($rgIds))));
for($i=0; $i<$iLoop; $i++)
{
$rgResult=array_merge($rgResult, $rgIds);
}
shuffle($rgResult);
此示例将通过此测试产生:
var_dump($rgResult, array_count_values($rgResult));
在以下输出中:
数组(30){
[0]=>
整数(93)
[1]=>
整数(93)
[2]=>
整数(5)
[3]=>
整数(93)
[4]=>
整数(10)
[5]=>
整数(72)
[6]=>
整数(10)
[7]=>
整数(5)
[8]=>
整数(72)
[9]=>
整数(10)
[10]=>
整数(5)
[11]=>
整数(93)
[12]=>
整数(72)
[13]=>
整数(5)
[14]=>
整数(72)
[15]=>
整数(10)
[16]=>
整数(5)
[17]=>
整数(10)
[18]=>
整数(93)
[19]=>
整数(93)
[20]=>
整数(93)
[21]=>
整数(72)
[22]=>
整数(5)
[23]=>
整数(93)
[24]=>
整数(72)
[25]=>
整数(72)
[26]=>
整数(10)
[27]=>
整数(5)
[28]=>
整数(10)
[29]=>
整数(72)
}
数组(4){
[93]=>
整数(8)
[5]=>
整数(7)
[10]=>
整数(7)
[72]=>
整数(8)
}