I have an array:
Array
(
[1] => 25
[2] => 50
[3] => 25
)
I would like to make it into:
Array
(
[1] => 50
[2] => 50
)
To do this I split the middle value between 1 and 3. This is the simplest example, where the split is 50,50. I would like to be able to take a 15 element array down to 6 elements.
Any ideas?
Additional Examples [10, 15, 20, 25] Reduced to two elements: 25(10 + 15),45(20 + 25) [10, 10, 10, 10, 11] Reduced to two elements: 25(10 + 10 + (10/2)),26((10/2) + 10 + 11)
After doing additional tests on Peter's solution, I noticed it did not get me what I expected if the reduce to size is an odd number. Here is the function I came up with. It also inflates data sets that are smaller then the requested size.
<?php
function reduceto($data,$r) {
$c = count($data);
// just enough data
if ($c == $r) return $data;
// not enough data
if ($r > $c) {
$x = ceil($r/$c);
$temp = array();
foreach ($data as $v) for($i = 0; $i < $x; $i++) $temp[] = $v;
$data = $temp;
$c = count($data);
}
// more data then needed
if ($c > $r) {
$temp = array();
foreach ($data as $v) for($i = 0; $i < $r; $i++) $temp[] = $v;
$data = array_map('array_sum',array_chunk($temp,$c));
}
foreach ($data as $k => $v) $data[$k] = $v / $r;
return $data;
}
?>