Unique Combinations
This algorithm finds all unique combinations in a specified list of values.
function GenerateCombinations($arr)
{
$combinations = array();
$length = count($arr);
for ($i = 0; $i < (1 << $length); ++$i)
{
$combination = array();
$count = 0;
for ($count = 0; $count < $length; ++$count)
{
if (($i & 1 << $count) > 0)
$combination[] = $arr[$count];
}
if ($count > 0 && count($combination) > 0)
$combinations[] = $combination;
}
return $combinations;
}
Example
$arr = array("How", "Are", "You");
$combinations = GenerateCombinations($arr);
Output
{"How"}
{"Are"}
{"How", "Are"}
{"You"}
{"How", "You"}
{"Are", "You"}
{"How", "Are", "You"}