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 '<br/>'; 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);
时间: 2024-11-12 19:55:00