php - multiple combinations in an Array -
i have array, amount of items in array can change. trying find different combinations values inside array, example array has these values;
array ( [0] => 60.0 [1] => 56.8 [2] => 42.08 [3] => 52.16 [4] => 52.8 )
is possible count number of values (in case 5) this;
60.0 + 56.8
60.0 + 56.8 + 42.08
60.0 + 56.8 + 42.08 + 52.16
60.0 + 56.8 + 42.08 + 52.16 + 52.8
but show combinations such as;
56.8 + 42.08
42.08 + 52.16
etc
i have tried using multi dimensional arrays, array shifting , other array related code.
simple sum up, pop last element.
will produce expected output, reversed. (you can add result array , resort, if required)
//untestet while (count($myarray) > 1)){ $current = 0; foreach ($myarray $e){ $current += $e; } echo "a result : " . $current; //remove last entry array_pop($myarray); }
this modify array. do
$myarray = array(1,2,3); ($i=0; $i<count($myarray); $i++){ $sums_until_index[$i] = 0; ($k=0; $k<=$i; $k++){ $sums_until_index[$i] += $myarray[$k]; } } print_r($sums_until_index); //array ( [0] => 1 [1] => 3 [2] => 6 )
Comments
Post a Comment