Return to Snippet

Revision: 40634
at February 4, 2011 16:16 by vinotht


Initial Code
function pc_array_power_set($array) {
    // initialize by adding the empty set
    $results = array(array( ));

    foreach ($array as $element)
        foreach ($results as $combination)
            array_push($results, array_merge(array($element), $combination));

    return $results;
}
This returns an array of arrays holding every combination of elements, including the empty set. For example: 
$set = array('A', 'B', 'C');
$power_set = pc_array_power_set($set);
$power_set contains eight arrays:
array( );
array('A');
array('B');
array('C');
array('A', 'B');
array('A', 'C');
array('B', 'C');
array('A', 'B', 'C');

Initial URL
array element combination

Initial Description
// Finding All Element Combinations of an Array
Buy Phentermine on line. Cheap Phentermine fedEx. Buy Phentermine prescription online. Overnight Phentermine cod. Order prescription Adderall. Cheap Adderall for sale online no prescription required. Adderall pharmacy cod saturday delivery. Cheap Adderall overnight.

Initial Title
All Element Combinations in an Array

Initial Tags


Initial Language
PHP