2016-04-21 73 views
0

我不知道如何命名這一點,以及如何做到這一點:合併和重新排列3個排列

<?php 
    $a = ['a1','a2','a3','a4']; // and more a5 … a42 
    $b = ['b1','b2','b3','b4']; 
    $c = ['c1','c2','c3','c4']; 

// want 

$full = ['a1','b1','c1', 
     'c2','a2','b2', 
     'b3','c3','a3', 
     'a4','b4','c4']; 

print_r($full); 

這就像一個bÇ,A,B,ç一個b,C,A,bç一個和溶液是一種數學本身里斯?

+2

參見:http://stackoverflow.com/q/797251/3933332 – Rizier123

+0

這就是所謂的** **變調。 –

+0

@PraveenKumar仔細看,它不完全像那個笨蛋。看看所需輸出的第二行。我也沒有看到第一次看。 – Rizier123

回答

1

首先我們創建三個原始數組多維數組,然後我們通過兩個嵌套循環for填充目的地$result

$all = [ $a, $b, $c ]; 
$result = []; 
for($i=0; $i<count($a); $i++) 
{ 
    for($j=0; $j<count($all); $j++) 
    { 
     $result[] = $all[ 3*(($i+$j*2)%3>0)-(($i+$j*2)%3) ][ $i ]; 
    } 
} 

eval.in demo

要填充$result,第二個鍵[$i]是每個原始數組(0,1,2,...)的漸進索引;創建第一個關鍵,我們用這個算法:

3 * (($i + $j * 2) % 3 > 0) - (($i + $j * 2) % 3)  $i $j 
-------------------------------------------------------- 
3 * ((0 + 0 * 2) % 3 > 0) - (( 0 + 0 * 2) % 3)  0 0 
3 * ((   0) % 3 > 0) - ((   0) % 3)  
3 * (      0) - (     0) = 0 

3 * ((0 + 1 * 2) % 3 > 0) - (( 0 + 1 * 2) % 3)  0 1 
3 * ((   2) % 3 > 0) - ((   2) % 3)  
3 * (      1) - (     2) = 1 

3 * ((0 + 2 * 2) % 3 > 0) - (( 0 + 2 * 2) % 3)  0 2 
3 * ((   4) % 3 > 0) - ((   4) % 3)  
3 * (      1) - (     1) = 2 
-------------------------------------------------------- 
3 * ((1 + 0 * 2) % 3 > 0) - (( 1 + 0 * 2) % 3)  1 0 
3 * ((   1) % 3 > 0) - ((   1) % 3)  
3 * (      1) - (     1) = 2 

3 * ((1 + 1 * 2) % 3 > 0) - (( 1 + 1 * 2) % 3)  1 1 
3 * ((   3) % 3 > 0) - ((   3) % 3)  
3 * (      0) - (     0) = 0 

3 * ((1 + 2 * 2) % 3 > 0) - (( 1 + 2 * 2) % 3)  1 2 
3 * ((   5) % 3 > 0) - ((   5) % 3)  
3 * (      1) - (     2) = 1 
-------------------------------------------------------- 
3 * ((2 + 0 * 2) % 3 > 0) - (( 2 + 0 * 2) % 3)  2 0 
3 * ((   2) % 3 > 0) - ((   2) % 3)  
3 * (      1) - (     2) = 1 

3 * ((2 + 1 * 2) % 3 > 0) - (( 2 + 1 * 2) % 3)  2 1 
3 * ((   4) % 3 > 0) - ((   4) % 3)  
3 * (      1) - (     1) = 2 

3 * ((2 + 2 * 2) % 3 > 0) - (( 2 + 2 * 2) % 3)  2 2 
3 * ((   6) % 3 > 0) - ((   6) % 3)  
3 * (      0) - (     0) = 0 
-------------------------------------------------------- 
3 * ((3 + 0 * 2) % 3 > 0) - (( 3 + 0 * 2) % 3)  3 0 
3 * ((   3) % 3 > 0) - ((   3) % 3)  
3 * (      0) - (     0) = 0 

3 * ((3 + 1 * 2) % 3 > 0) - (( 3 + 1 * 2) % 3)  3 1 
3 * ((   5) % 3 > 0) - ((   5) % 3)  
3 * (      1) - (     2) = 1 

3 * ((3 + 2 * 2) % 3 > 0) - (( 3 + 2 * 2) % 3)  3 2 
3 * ((   7) % 3 > 0) - ((   7) % 3)  
3 * (      1) - (     1) = 2 
-------------------------------------------------------- 
(...) 
2
$arr = [$a, $b, $c];  
$full = []; 
$i = 0; 
$k = 0; 
for ($j = 0; $j < count($a); $j++) 
    do { 
    $full[] = $arr[$k][$j]; 
    $i = ++$i % 3; 
    if ($i) $k = ++$k % 3; 
    } while ($i); 
print_r($full); 

demo

3
Need to merge all the array, Please go through this PHP code. 

    $a = array('a1','a2','a3','a4'); // and more a5 … a42 
    $b = array('b1','b2','b3','b4'); 
    $c = array('c1','c2','c3','c4');  
    $d = array_merge($a,$b,$c); 
    echo mathseries($d,3); 

    function mathseries($d=array(),$break_number) { 
     $f = count($d)/$break_number; 
     for($i=0;$i<$f;$i++) { 
      $r[] = $d[$i].','.$d[$i+$f].','.$d[$i+($f*2)]; 
     } 
     $r = implode(',',$r); 
     return $r; 
    } 
create a function in which two parameter send, 1st parameter array. 2nd parameter is break point.