0

I currently have an array, the amount of items in the array can change. I am trying to find different combinations for the values inside the array, for example the array currently has these values;

Array ( [0] => 60.0 [1] => 56.8 [2] => 42.08 [3] => 52.16 [4] => 52.8 )

Is it possible to count the number of values (in this case 5) then do something like 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 also to show all combinations such as;
56.8 + 42.08
42.08 + 52.16
etc

I have tried using multi dimensional arrays, array shifting and other array related code.

Alex
  • 493
  • 1
  • 6
  • 11

4 Answers4

0

this example echos the sum and the equotation of the given array:

$array = array(1,2,3,4);
$total = 0;
$equot = array();
foreach($array as $k=>$a) {     
   $total += $a;
   $equot[] = $a;
   if($k>0) { 
       echo implode("+",$equot)." = $total<br>"; 
   }
}

for the given array it echoes:

  • 1+2 = 3
  • 1+2+3 = 6
  • 1+2+3+4 = 10

if you like to get all possible combinations you should read this existing thread:

PHP: How to get all possible combinations of 1D array?

Community
  • 1
  • 1
steven
  • 4,868
  • 2
  • 28
  • 58
  • Yes but not so much as a running total, more of a show each combination , for example; array(1,2,3,4) and I would be looking for the answers to; 1 + 2 then 1 + 2 + 3 then 1 + 2 + 3 + 4 etc – Alex Aug 07 '13 at 13:30
0
$sum = array_sum(array_slice($array, 0, rand(1, count($array))));

UPDATE:

The below code will accept an $array with the numbers, and then display the sum of random (starting from first, without duplicates) elements along with the equation.

$rand = rand(1, count($array));
$sum = array_sum(array_slice($array, 0, $rand));
$equation = implode(' + ', array_slice($array, 0, $rand));

echo $equation. ' = '. $sum;

In action: Codepad

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

This is just an example printing the equotation. If you want it to calculate, modify it.

<?php

$array = Array(10, 20, 30, 40, 50);

for($i = 1; $i < count($array); $i++) {
    $sum = "";
    for($x = 0; $x <= $i; $x++) {
        $sum = $sum . $array[$x] . ($x != $i ? " + " : "");
    }
    echo $sum . "\r\n";
}

Working demo: http://codepad.org/ZgkdW2d5

Matthijs
  • 568
  • 2
  • 11
  • Would it be possible to show all combinations so it shows 10 + 20 + 30, but could it also show 20 + 30? – Alex Aug 07 '13 at 13:46
0

Simple sum it up, then pop last element.

Will produce your expected output, just reversed. (You can add the result to another array and resort, if required)

//untestet
while (count($myArray) > 1)){
  $current = 0;
  foreach ($myArray AS $e){
    $current += $e;
  }
  echo "A Result : " . $current;

  //remove last entry
  array_pop($myArray);
}

This however will modify your array. You could also do

$myArray = Array(1,2,3);
for ($i=0; $i<count($myArray); $i++){
  $sums_until_index[$i] = 0;
  for ($k=0; $k<=$i; $k++){
    $sums_until_index[$i] += $myArray[$k];  
  }

}

print_r($sums_until_index); //Array ( [0] => 1 [1] => 3 [2] => 6 )
dognose
  • 20,360
  • 9
  • 61
  • 107