GepComty

kombinasi

Dec 11th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. <?php
  2. function getCombinations($base,$n){
  3. $baselen = count($base);
  4. if($baselen == 0){
  5. return;
  6. }
  7. if($n == 1){
  8. $return = array();
  9. foreach($base as $b){
  10. $return[] = array($b);
  11. }
  12. return $return;
  13. }else{
  14. $oneLevelLower = getCombinations($base,$n-1);
  15.  
  16. $newCombs = array();
  17.  
  18. foreach($oneLevelLower as $oll){
  19.  
  20. $lastEl = $oll[$n-2];
  21. $found = false;
  22. foreach($base as $key => $b){
  23. if($b == $lastEl){
  24. $found = true;
  25. continue;
  26.  
  27. }
  28. if($found == true){
  29. if($key < $baselen){
  30. $tmp = $oll;
  31. $newCombination = array_slice($tmp,0);
  32. $newCombination[]=$b;
  33. $newCombs[] = array_slice($newCombination,0);
  34. }
  35. }
  36. }
  37. }
  38. }
  39. return $newCombs;
  40. }
  41. $set=array(1, 2, 3, 4);
  42. $count = count($set);
  43. $result = array();
  44. for($i = 1; $i <= $count; $i++){
  45. $result[] = getCombinations($set, $i);
  46. }
  47. echo "<pre>";
  48. print_r($result);
  49. echo "</pre>";
  50. $res = count($result);
  51. for($i=0;$i<$res;$i++){
  52. for($f=0;$f<$res;$f++){
  53. for($g=0;$g<$res;$g++){
  54. echo $result[$i][$f][$g];
  55. }
  56. }
  57. }
  58.  
  59.  
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment