直接上代码
<?php
function getCombinationToString($arr,$m){
$result = array();
if ($m==1){
return $arr;
}
if($m == count($arr)){//当取出的个数等于数组的长度,就是只有一种组合,即本身
$result[] = implode(',',$arr);
return $result;
}
$temp_firstelement = $arr[0];
unset($arr[0]);
$arr = array_values($arr);
$temp_first1 = getCombinationToString($arr,$m - 1);
foreach($temp_first1 as $s){
$s = $temp_firstelement.','.$s;
$result[] = $s;
}
unset($temp_first1);
$temp_first2 = getCombinationToString($arr,$m);
foreach($temp_first2 as $s){
$result[] = $s;
}
unset($temp_first2);
return $result;
}
$arr = range(1, 6);//1~6的数组
$result = getCombinationToString($arr,2);//6个数里面,取出2个数有多少种组合(即不考虑顺序)
$arr=[1,2,3,4,5,6,7,8,9];
var_dump(getCombinationToString($arr,4));
$data['count'] = count($result);//组合种数
$data['data'] = $result;//各种数据组合
echo "<pre>";
print_r($data);
echo "</pre>";