Skip to content

Instantly share code, notes, and snippets.

@christophervalles
Created July 6, 2011 08:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophervalles/1066801 to your computer and use it in GitHub Desktop.
Save christophervalles/1066801 to your computer and use it in GitHub Desktop.
power set of and array
<?php
function pc_array_power_set($array, $limit){
$subset = array();
$results = array(array());
foreach ($array as $element){
foreach ($results as $combination){
$result = array_merge(array($element), $combination);
array_push($results, $result);
if(count($result) == $limit){
$subset[] = $result;
}
}
}
return $subset;
}
$set = range(1, 15);
print_r(pc_array_power_set($set, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment