Advertisement
Guest User

Combinations, php version

a guest
Jun 20th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1. $result = array();
  2. $combination = array();
  3.  
  4. function combinations(array $myArray, $choose) {
  5.   global $result, $combination;
  6.  
  7.   $n = count($myArray);
  8.  
  9.   function inner ($start, $choose_, $arr, $n) {
  10.     global $result, $combination;
  11.  
  12.     if ($choose_ == 0) array_push($result,$combination);
  13.     else for ($i = $start; $i <= $n - $choose_; ++$i) {
  14.            array_push($combination, $arr[$i]);
  15.            inner($i + 1, $choose_ - 1, $arr, $n);
  16.            array_pop($combination);
  17.          }
  18.   }
  19.   inner(0, $choose, $myArray, $n);
  20.   return $result;
  21. }
  22.  
  23. $comb = combinations(array(1,2,3,4,5,6,7,8,9,10), 5);
  24.    foreach ( $comb as $c) {
  25.       foreach ( $c as $e ) echo "$e ";
  26.      echo "<br />";
  27.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement