Linked Questions

Popular Questions

How to find sum of each subset in php?

Asked by At

I want to compute the sum of all possible sub-set form array.

$array= Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 6 ); //changing

function powerSet($array) {
 // add the empty set
    $results = array(array());
    foreach ($array as $element) {
        foreach ($results as $combination) {
            $results[] = array_merge(array($element), $combination);
            $total= array_sum($results); // I try this
        }
        echo $total; // I try this
    }
    return $results;
}

The above code is used to find subsets. I found this code from here. I just add array_sum but show 0's how to find each subset total? any way ?

Related Questions