The rest of this function relies on the nature of combinations and
PHP's implementation of foreach.
Each new element added to the array increases the number of
combinations. The new combinations are all the old combinations
alongside the new element; a two-element array containing
A and B generates four possible
combinations: {}, {A},
{B}, and {A,
B}. Adding C to this set keeps
the four previous combinations but also adds four new combinations:
{C}, {A, C},
{B, C}, and
{A, B, C}.
To remove the empty set, replace the opening line of:
// initialize by adding the empty set
$results = array(array( ));
with:
// initialize by adding the first element
$results = array(array(array_pop($array)));
Since a one-element array has only one
combination — itself — popping off an element is identical to
making the first pass through the loop. The double
foreach statements don't know
they're really starting their processing with the
second element in the array.
To print the results with tabs between elements inside the
combination and returns between each combination, use the following:
$array = array('Adam', 'Bret', 'Ceff', 'Dave');
foreach (pc_array_power_set($array) as $combination) {
print join("\t", $combination) . "\n";
}
Here's how to print only three-element sized
combinations:
foreach (pc_array_power_set($set) as $combination) {
if (3 == count($combination)) {
print join("\t", $combination) . "\n";
}
}
Iterating over a large set of elements takes a long time. A set of
n elements generates
2n+1 sets. In other words, as
n grows by 1, the number of elements doubles.