Array balanced interlace

array_balanced_interlace
배열 균형 배치
배열 균형 병합

1 소스코드[ | ]

<?php
function xmp($arr) { echo '<xmp>'; print_r($arr); echo '</xmp>'; }
function array_balanced_interlace($arr1, $arr2) {
	if(count($arr2)>count($arr1)) list($arr1,$arr2)=array($arr2,$arr1);

	$cnt1 = count($arr1);
	$cnt2 = count($arr2);
	$interval = $cnt1/$cnt2;

	for($i=$cnt2-1;$i>-1;$i--) {
		$pos = round(($i+0.5)*$interval);
		array_splice($arr1, $pos, 0, $arr2[$i]);
	}
	return $arr1;
}

$as = array('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7');
$bs = array('B1', 'B2');
$cs = array('C1', 'C2');
$ds = array('D1');

$arr = array_balanced_interlace($as, $bs);
$arr = array_balanced_interlace($arr, $cs);
$arr = array_balanced_interlace($arr, $ds);
xmp($arr);

2 실행결과[ | ]

Array
(
    [0] => A1
    [1] => A2
    [2] => C1
    [3] => B1
    [4] => A3
    [5] => A4
    [6] => D1
    [7] => A5
    [8] => B2
    [9] => C2
    [10] => A6
    [11] => A7
)

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}