合并并重新排序3个数组

时间:2016-04-21 18:08:38

标签: php arrays algorithm logic

我不知道如何命名,以及如何命名:

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

它类似于 a b c ,a,b, c ,< strong> a , b ,c,a, b c a ,解决方案是一个数学系列?

3 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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

答案 2 :(得分:1)

首先我们创建一个包含三个原始数组的多维数组,然后我们通过两个嵌套的<button onclick="settime();">Set Time</button> <div class="container"> <video id="video1" class="video-js vjs-default-skin" muted> <source src="video.m4v" type="video/mp4" /> HTML5 Video is required for this example. </video> 循环填充目标$result

for

eval.in demo

要填充$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 ]; } } ,第二个键$result是每个原始数组的渐进索引(0,1,2,...);要创建第一个密钥,我们使用此算法:

[$i]