时间:2021-07-01 10:21:17 帮助过:17人阅读
1、求给定数组的组合
如array('a', 'b', 'c')
结果为
a
b
c
a b
a c
b c
function combination($arr){ if(empty($arr)){ return false; } $count = count($arr); for($i=1; $i<$count; $i++){ combinationResult($arr, 0, $count, $i); }}function combinationResult($arr, $begin, $end, $number){ static $result = array(); if($number == 0){ $count = count($result); for($i=0; $i<$count; $i++){ echo $result[$i] . ' '; } echo '
'; return true; } if($begin == $end){ return false; } array_push($result, $arr[$begin]); combinationResult($arr, $begin+1, $end, $number-1); array_pop($result); combinationResult($arr, $begin+1, $end, $number);}$arr = array('a', 'b', 'c', 'd');combination($arr);