Created
July 6, 2011 08:05
-
-
Save christophervalles/1066801 to your computer and use it in GitHub Desktop.
power set of and array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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