我有兩個隨機數組合並,一切都顯示/運行的很好。我堅持的是這樣的:合併陣列與陣列位置的控制
當我將兩個數組合並在一起時,我需要確保每個3項來自array2。
array1 (a,b,c,d,e,f,g,h,i,j,k)
array2 (a1,a2,a3,a4)
我想要的結果:
d k c a1 j i g a2
等
我迄今所做的:
function randomize_blocks($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
//gather donor blocks into an array
$donorBlocks = array($blockA, $blockB, $blockC, $blockD, $blockE, $blockF,
$blockG, $blockH, $blockI, $blockJ, $blockK, $blockL, $blockM, $blockN,
$blockO);
//gather value blocks into an array
$valueBlocks = array($valueA, $valueB, $valueC, $valueD);
//shake that shuffler real hard! DT
$shuffled_valueBlocks = randomize_blocks($valueBlocks, 4);
$shuffled_donorBlocks = randomize_blocks($donorBlocks, 15);
//combine our shuffled arrays together
$combinedArrays = array_merge($shuffled_valueBlocks, $shuffled_donorBlocks);
//shuffle them all together!
$shuffled_blocks = randomize_blocks($combinedArrays, 19);
//display bocks on page
foreach ($combinedArrays as $key => $value) {
echo $value;
}
什麼是實現這一目標的最佳方式是什麼?
合併之後您想要的結果是什麼?請張貼。也是迄今爲止您的嘗試? –
現在編輯帖子。 – Johanna
等待時間8個值會結果? –