<?php
/**
* @param $arr 需要组合数组
* @param int $num 组合元素个数
* @return array
*/
function getSequenceAry($arr,$num=1)
{
$count = count($arr);
$min = min($count,$num);
if($min<1){
return false;
}
$return =array();
for(;$min>=1;$min--){
//此处如果改成for(;$min>1;$min--)只输出随机取出两个元素的重复组合
$arrRet = array();
$max = $count-($min-1);
for($i=0;$i<$max;$i++){
getSequenceArySub($arr,$count,$min,$i,$arrRet,$return);
}
}
return $return;
}
function getSequenceArySub($arr,$count,$min,$i,$arrRet=array(),& $return){
if(empty($arr) || empty($count))
return false;
if(1==$min){
$arrRet[--$min] = $arr[$i];
$return[] = $arrRet;
}else{
$arrRet[--$min] = $arr[$i];
for($j = $i+1;$j<($count);$j++){
getSequenceArySub($arr,$count,$min,$j,$arrRet,$return);
}
}
}
print_r(getSequenceAry(array(1,2,3,4,5),1));
print_r(getSequenceAry(array(1,2,3,4,5),2));
print_r(getSequenceAry(array(1,2,3,4,5),3));
print_r(getSequenceAry(array(1,2,3,4,5),4));
print_r(getSequenceAry(array(1,2,3,4,5),5));
?>
结果:
Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 4
)
[4] => Array
(
[0] => 5
)
)
Array
(
[0] => Array
(
[1] => 1
[0] => 2
)
[1] => Array
(
[1] => 1
[0] => 3
)
[2] => Array
(
[1] => 1
[0] => 4
)
[3] => Array
(
[1] => 1
[0] => 5
)
[4] => Array
(
[1] => 2
[0] => 3
)
[5] => Array
(
[1] => 2
[0] => 4
)
[6] => Array
(
[1] => 2
[0] => 5
)
[7] => Array
(
[1] => 3
[0] => 4
)
[8] => Array
(
[1] => 3
[0] => 5
)
[9] => Array
(
[1] => 4
[0] => 5
)
[10] => Array
(
[0] => 1
)
[11] => Array
(
[0] => 2
)
[12] => Array
(
[0] => 3
)
[13] => Array
(
[0] => 4
)
[14] => Array
(
[0] => 5
)
)
Array
(
[0] => Array
(
[2] => 1
[1] => 2
[0] => 3
)
[1] => Array
(
[2] => 1
[1] => 2
[0] => 4
)
[2] => Array
(
[2] => 1
[1] => 2
[0] => 5
)
[3] => Array
(
[2] => 1
[1] => 3
[0] => 4
)
[4] => Array
(
[2] => 1
[1] => 3
[0] => 5
)
[5] => Array
(
[2] => 1
[1] => 4
[0] => 5
)
[6] => Array
(
[2] => 2
[1] => 3
[0] => 4
)
[7] => Array
(
[2] => 2
[1] => 3
[0] => 5
)
[8] => Array
(
[2] => 2
[1] => 4
[0] => 5
)
[9] => Array
(
[2] => 3
[1] => 4
[0] => 5
)
[10] => Array
(
[1] => 1
[0] => 2
)
[11] => Array
(
[1] => 1
[0] => 3
)
[12] => Array
(
[1] => 1
[0] => 4
)
[13] => Array
(
[1] => 1
[0] => 5
)
[14] => Array
(
[1] => 2
[0] => 3
)
[15] => Array
(
[1] => 2
[0] => 4
)
[16] => Array
(
[1] => 2
[0] => 5
)
[17] => Array
(
[1] => 3
[0] => 4
)
[18] => Array
(
[1] => 3
[0] => 5
)
[19] => Array
(
[1] => 4
[0] => 5
)
[20] => Array
(
[0] => 1
)
[21] => Array
(
[0] => 2
)
[22] => Array
(
[0] => 3
)
[23] => Array
(
[0] => 4
)
[24] => Array
(
[0] => 5
)
)
Array
(
[0] => Array
(
[3] => 1
[2] => 2
[1] => 3
[0] => 4
)
[1] => Array
(
[3] => 1
[2] => 2
[1] => 3
[0] => 5
)
[2] => Array
(
[3] => 1
[2] => 2
[1] => 4
[0] => 5
)
[3] => Array
(
[3] => 1
[2] => 3
[1] => 4
[0] => 5
)
[4] => Array
(
[3] => 2
[2] => 3
[1] => 4
[0] => 5
)
[5] => Array
(
[2] => 1
[1] => 2
[0] => 3
)
[6] => Array
(
[2] => 1
[1] => 2
[0] => 4
)
[7] => Array
(
[2] => 1
[1] => 2
[0] => 5
)
[8] => Array
(
[2] => 1
[1] => 3
[0] => 4
)
[9] => Array
(
[2] => 1
[1] => 3
[0] => 5
)
[10] => Array
(
[2] => 1
[1] => 4
[0] => 5
)
[11] => Array
(
[2] => 2
[1] => 3
[0] => 4
)
[12] => Array
(
[2] => 2
[1] => 3
[0] => 5
)
[13] => Array
(
[2] => 2
[1] => 4
[0] => 5
)
[14] => Array
(
[2] => 3
[1] => 4
[0] => 5
)
[15] => Array
(
[1] => 1
[0] => 2
)
[16] => Array
(
[1] => 1
[0] => 3
)
[17] => Array
(
[1] => 1
[0] => 4
)
[18] => Array
(
[1] => 1
[0] => 5
)
[19] => Array
(
[1] => 2
[0] => 3
)
[20] => Array
(
[1] => 2
[0] => 4
)
[21] => Array
(
[1] => 2
[0] => 5
)
[22] => Array
(
[1] => 3
[0] => 4
)
[23] => Array
(
[1] => 3
[0] => 5
)
[24] => Array
(
[1] => 4
[0] => 5
)
[25] => Array
(
[0] => 1
)
[26] => Array
(
[0] => 2
)
[27] => Array
(
[0] => 3
)
[28] => Array
(
[0] => 4
)
[29] => Array
(
[0] => 5
)
)
Array
(
[0] => Array
(
[4] => 1
[3] => 2
[2] => 3
[1] => 4
[0] => 5
)
[1] => Array
(
[3] => 1
[2] => 2
[1] => 3
[0] => 4
)
[2] => Array
(
[3] => 1
[2] => 2
[1] => 3
[0] => 5
)
[3] => Array
(
[3] => 1
[2] => 2
[1] => 4
[0] => 5
)
[4] => Array
(
[3] => 1
[2] => 3
[1] => 4
[0] => 5
)
[5] => Array
(
[3] => 2
[2] => 3
[1] => 4
[0] => 5
)
[6] => Array
(
[2] => 1
[1] => 2
[0] => 3
)
[7] => Array
(
[2] => 1
[1] => 2
[0] => 4
)
[8] => Array
(
[2] => 1
[1] => 2
[0] => 5
)
[9] => Array
(
[2] => 1
[1] => 3
[0] => 4
)
[10] => Array
(
[2] => 1
[1] => 3
[0] => 5
)
[11] => Array
(
[2] => 1
[1] => 4
[0] => 5
)
[12] => Array
(
[2] => 2
[1] => 3
[0] => 4
)
[13] => Array
(
[2] => 2
[1] => 3
[0] => 5
)
[14] => Array
(
[2] => 2
[1] => 4
[0] => 5
)
[15] => Array
(
[2] => 3
[1] => 4
[0] => 5
)
[16] => Array
(
[1] => 1
[0] => 2
)
[17] => Array
(
[1] => 1
[0] => 3
)
[18] => Array
(
[1] => 1
[0] => 4
)
[19] => Array
(
[1] => 1
[0] => 5
)
[20] => Array
(
[1] => 2
[0] => 3
)
[21] => Array
(
[1] => 2
[0] => 4
)
[22] => Array
(
[1] => 2
[0] => 5
)
[23] => Array
(
[1] => 3
[0] => 4
)
[24] => Array
(
[1] => 3
[0] => 5
)
[25] => Array
(
[1] => 4
[0] => 5
)
[26] => Array
(
[0] => 1
)
[27] => Array
(
[0] => 2
)
[28] => Array
(
[0] => 3
)
[29] => Array
(
[0] => 4
)
[30] => Array
(
[0] => 5
)
)
您可以选择一种方式赞助本站
赏