0

I don't know how to name this, and how to do it:

<?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);

It's like a,b,c,a,b,c,a,b,c,a,b,c,a and solution is a mathematical series?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
benoît
  • 1,473
  • 3
  • 13
  • 31

3 Answers3

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Gaurav Mahajan
  • 290
  • 1
  • 5
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

splash58
  • 26,043
  • 3
  • 22
  • 34
1

First we create a multidimensional array with three original arrays, then we populate destination $result through two nested for loops:

$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 ];
    }
}

3v4l.org demo

To populate $result, the second key [$i] is the progressive index of each original array (0, 1, 2, ...); to create first key we use this algorithm:

 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
 --------------------------------------------------------
 (...)
fusion3k
  • 11,568
  • 4
  • 25
  • 47