这是我的建议。。。
0
和
40
$data
)和分割值之和(
$data_divided
),我建议将因子四舍五入到下一个整数(
ceil()
$divided_data
带减法或
abs()
.
我的演示将存储并显示初始值、计算值以及它们之间的数学差。正如您将在我的注释和输出中看到的,只有带有索引的子阵列
9
所有计算数据均在可接受范围内,最大值为
38
和一分钟
0.6
这种方法不能保证最大值为
但这将给你的数据提供一个很好的机会,让我准确地加起来。如果您删除
来自的函数
$factor
40
变得可能,但也有一些价值观“摩擦”(一些比其他更糟糕)。
Demo
)
$data = [76,190,120,39,56,11,17,67,45,23,76,54];
// notice flawed data-----------------^^
$data_divided = [[32,14,30],[100,80,10],[50,40,30],[20,10,9],[6,40,10],[3,5,3],[10,4,3],[50,10,7],[30,10,5],[10,20,3],[30,40,6],[4,25,25]];
// notice flawed data----------------------------------------------------------------------------------------^^^^^^^
$factor=ceil(max($data)/40); // 5 in this case (rounds up to a clean integer for future dividing)
$n_data = array();
foreach ($data_divided as $i=>$a) {
$n_value=$data[$i]/$factor; // reduce expected sum value
$a=array_map(function($v)use($factor){return $v/$factor;},$a); // reduce split values
$sum=array_sum($a); // sum the reduced split values
$n_data[]=[
"N_Value"=>$n_value,
"Split"=>$a,
"Sum"=>$sum,
"Diff"=>$n_value-$sum // find difference in calculated values
];
}
$data = $n_data;
var_export($data);
array (
0 =>
array (
'N_Value' => 15.2,
'Split' =>
array (
0 => 6.4,
1 => 2.8,
2 => 6.0,
),
'Sum' => 15.2,
'Diff' => 0.0,
),
1 =>
array (
'N_Value' => 38.0,
'Split' =>
array (
0 => 20.0,
1 => 16.0,
2 => 2.0,
),
'Sum' => 38.0,
'Diff' => 0.0,
),
2 =>
array (
'N_Value' => 24.0,
'Split' =>
array (
0 => 10.0,
1 => 8.0,
2 => 6.0,
),
'Sum' => 24.0,
'Diff' => 0.0,
),
3 =>
array (
'N_Value' => 7.8,
'Split' =>
array (
0 => 4.0,
1 => 2.0,
2 => 1.8,
),
'Sum' => 7.8,
'Diff' => 0.0,
),
4 =>
array (
'N_Value' => 11.2,
'Split' =>
array (
0 => 1.2,
1 => 8.0,
2 => 2.0,
),
'Sum' => 11.2,
'Diff' => 0.0,
),
5 =>
array (
'N_Value' => 2.2,
'Split' =>
array (
0 => 0.6,
1 => 1.0,
2 => 0.6,
),
'Sum' => 2.2,
'Diff' => 0.0,
),
6 =>
array (
'N_Value' => 3.4,
'Split' =>
array (
0 => 2.0,
1 => 0.8,
2 => 0.6,
),
'Sum' => 3.4,
'Diff' => 0.0,
),
7 =>
array (
'N_Value' => 13.4,
'Split' =>
array (
0 => 10.0,
1 => 2.0,
2 => 1.4,
),
'Sum' => 13.4,
'Diff' => 0.0,
),
8 =>
array (
'N_Value' => 9.0,
'Split' =>
array (
0 => 6.0,
1 => 2.0,
2 => 1.0,
),
'Sum' => 9.0,
'Diff' => 0.0,
),
9 =>
array (
'N_Value' => 4.6,
'Split' =>
array (
0 => 2.0,
1 => 4.0,
2 => 0.6,
),
'Sum' => 6.6,
'Diff' => -2.0,
),
10 =>
array (
'N_Value' => 15.2,
'Split' =>
array (
0 => 6.0,
1 => 8.0,
2 => 1.2,
),
'Sum' => 15.2,
'Diff' => 0.0,
),
11 =>
array (
'N_Value' => 10.8,
'Split' =>
array (
0 => 0.8,
1 => 5.0,
2 => 5.0,
),
'Sum' => 10.8,
'Diff' => 0.0,
),
)