<?php
//从$arr数组中,获取$m个数字组成数组,也就是排列组合的C运算符
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个数有多少种组合(即不考虑顺序)
$data['count'] = count($result);//组合种数
$data['data'] = $result;//各种数据组合
echo "<pre>";
print_r($data);
echo "</pre>";
运行结果: