2017-08-29 26 views
1

我有兩個隨機數組合並,一切都顯示/運行的很好。我堅持的是這樣的:合併陣列與陣列位置的控制

當我將兩個數組合並在一起時,我需要確保每個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; 
    } 

什麼是實現這一目標的最佳方式是什麼?

+3

合併之後您想要的結果是什麼?請張貼。也是迄今爲止您的嘗試? –

+0

現在編輯帖子。 – Johanna

+0

等待時間8個值會結果? –

回答

2

這是基於要用作放置在你想要的圖案都是數組洗牌以及假設:

$array_one = array('a','b','c','d','e','f','g','h','i','j','k'); 
$array_two = array('a1','a2','a3','a4'); 

function merge_random_interval($array_one = array(), $array_two = array(), $interval = 3) { 
    $return_array = array(); 

    // Shuffle the arrays - that seems to be important? 
    shuffle($array_one); 
    shuffle($array_two); 

    // Go through the first array 
    $array_two_index = 0; 
    for ($i = 0; $i < count($array_one); $i++) { 

     // Every $interval add a value from the second array 
     if ($i > 0 && $i % $interval == 0) { 

      // Make sure there is a value in the second array 
      if (isset($array_two[$array_two_index])) { 
       $return_array[] = $array_two[$array_two_index]; 
       $array_two_index++; 
      } 
     } 

     $return_array[] = $array_one[$i]; 
    } 

    // You may want to check if $array_two has more values that weren't added? 
    // Compare $array_two_index against count($array_two) 

    return $return_array; 
} 

$result_array = merge_random_interval($array_one, $array_two, 3); 

echo implode(' ', $result_array); // g d c a1 h i j a4 b e a a2 k f 
+0

這似乎是我一直在尋找!現在測試。 – Johanna

+0

@Johanna沒有答案會給你19個值(如你在評論中所說的):-https://eval.in/852160 –

+0

@neuromatter我感謝你的幫助!輝煌!你拯救了一天! – Johanna

3

這樣的事情呢?

$a1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']; 
$a2 = ['a1', 'a2', 'a3', 'a4']; 


$a = []; 
shuffle($a1); 

foreach ($a1 as $k => $v) { 
    $a[] = $v; 
    if (($k + 1) % 3 === 0 && $a2) $a[] = array_shift($a2); 
} 
+0

我的天啊,真漂亮。現在讓我來修補它並提出建議。 – Johanna

+0

@Johanna沒有答案會給你19個值(如你在評論中所說的):-https://eval.in/852159 –

+0

@AlivetoDie這個例子不能輸出19個值,因爲沒有19個輸入值洗牌在一起。當'$ a1'有15個項目時,它會這樣做。 –

4

這裏是array_splice的方式來修改時間最長的兩個數組的成所期望的結果:

$a = ['a', 'b', 'c', 'd']; 
$b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; 

foreach ($a as $i => $v) 
    array_splice($b, $i*4+3, 0, [$v]); 

$b將是:

[1, 2, 3, 'a', 4, 5, 6, 'b', 7, 8, 9, 'c', 10, 11, 12, 'd', 13, 14, 15] 
+0

感謝您發佈優雅的解決方案!這將是一個不錯的登陸頁面,爲那些陷入類似的位置,我一樣 – Johanna

+1

哦,最好的解決方案,但來得晚一點。+ 1 BTW @trincot –