Q:

Split an array into three similar Sum

I would like to split an array into three array that have similar sums - as close as possible
I have array 
$arr = [1,2,4,7,1,6,2,8];

Desire output for example:
a = 8,2 // as sum is 10
b = 7,2,1 // as sum is 10
c = 6,4,1 // as sum is 10

Thanks 

A:

You can use the following algorithm:

Sort the input array from big to small
Create output array
for each element in the input - insert to the lowest sum in the output array.

Consider the following code:
$arr = [1,2,4,7,1,6,2,8];
sort($arr);
$arr = array_reverse($arr); // big to small
$out = array(array(),array(),array()); // output array

for($i = 0; $i < 8; $i++) {
    $sums = array_map("array_sum" ,$out); // get all current sums of the array 
    $index = array_keys($sums, min($sums))[0]; // get the min sum
    $out[$index][] = $arr[$i]; // add the element to the array with the lowest sum 
}

echo print_r($out, true);

Now you will get:
array:
  [0]: array:
         [0] => 8
         [1] => 2
         [2] => 1
  [1]: array:
         [0] => 7
         [1] => 2
         [2] => 1
   [2]: array:
         [0] => 6
         [1] => 4

