我想設置一個邏輯並計算一些值,但計算似乎是錯誤的。單個數值的總和應該如何等於數組中的父值?
UPDATE:
讓我解釋,例如我有[23,11,39]等,在陣列中的一系列值,然後在接下來的陣列我這些值除以成每個3個值第一個數組元素的值爲:對於23我將它分成3個值爲10,10,3,因此這3個值的總和等於後退23,然後與其他值相同,然後接下來我計算所有這些的值相對於40即,即將在40中剩餘多少,然後剩餘部分以及對於除法值也是相同的方式,所以問題在於分割值的總和應該等於父40的計算值,但它們不是當我將它們相加時。所以這就是問題所在!
你們能讓我知道我做錯了什麼,它是如何修復的?
這裏是我的代碼:
$data = [76,190,120,39,56,11,17,67,45,23,76,54];
$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]];
$min = min($data);
$max = max($data) - $min;
$n_data = array();
$i = 0;
foreach ($data_divided as $value) {
$n_value = (($data[$i] - $min)/$max)*40;
$n0 = (($value[0] - $min)/$max)*40;
$n1 = (($value[1] - $min)/$max)*40;
$n2 = (($value[2] - $min)/$max)*40;
$n_data[] = [$n_value => [abs($n0),abs($n1),abs($n2)], abs($n0)+abs($n1)+abs($n2) ];
echo "Parent Value : ".$n_value."<br>";
echo "n0 : ".abs($n0)."<br>";
echo "n1 : ".abs($n1)."<br>";
echo "n2 : ".abs($n2)."<br>";
echo "n0+n1+n2 : ".(abs($n0)+abs($n1)+abs($n2))."<br><br><br>";
$i++;
}
下面是它的輸出爲:
Parent Value : 14.525139664804
n0 : 4.6927374301676
n1 : 0.67039106145251
n2 : 4.2458100558659
n0+n1+n2 : 9.608938547486
Parent Value : 40
n0 : 19.888268156425
n1 : 15.418994413408
n2 : 0.22346368715084
n0+n1+n2 : 35.530726256983
Parent Value : 24.357541899441
n0 : 8.7150837988827
n1 : 6.4804469273743
n2 : 4.2458100558659
n0+n1+n2 : 19.441340782123
Parent Value : 6.2569832402235
n0 : 2.0111731843575
n1 : 0.22346368715084
n2 : 0.44692737430168
n0+n1+n2 : 2.6815642458101
Parent Value : 10.055865921788
n0 : 1.1173184357542
n1 : 6.4804469273743
n2 : 0.22346368715084
n0+n1+n2 : 7.8212290502793
Parent Value : 0
n0 : 1.7877094972067
n1 : 1.340782122905
n2 : 1.7877094972067
n0+n1+n2 : 4.9162011173184
Parent Value : 1.340782122905
n0 : 0.22346368715084
n1 : 1.5642458100559
n2 : 1.7877094972067
n0+n1+n2 : 3.5754189944134
Parent Value : 12.513966480447
n0 : 8.7150837988827
n1 : 0.22346368715084
n2 : 0.89385474860335
n0+n1+n2 : 9.8324022346369
Parent Value : 7.5977653631285
n0 : 4.2458100558659
n1 : 0.22346368715084
n2 : 1.340782122905
n0+n1+n2 : 5.8100558659218
Parent Value : 2.6815642458101
n0 : 0.22346368715084
n1 : 2.0111731843575
n2 : 1.7877094972067
n0+n1+n2 : 4.0223463687151
Parent Value : 14.525139664804
n0 : 4.2458100558659
n1 : 6.4804469273743
n2 : 1.1173184357542
n0+n1+n2 : 11.843575418994
Parent Value : 9.608938547486
n0 : 1.5642458100559
n1 : 3.1284916201117
n2 : 3.1284916201117
n0+n1+n2 : 7.8212290502793
預期輸出:
Parent Value : 14.525139664804
n0 : 4.6927374301676
n1 : 0.67039106145251
n2 : 4.2458100558659
n0+n1+n2 : 14.525139664804
Parent Value : 40
n0 : 19.888268156425
n1 : 15.418994413408
n2 : 0.22346368715084
n0+n1+n2 : 40
Parent Value : 24.357541899441
n0 : 8.7150837988827
n1 : 6.4804469273743
n2 : 4.2458100558659
n0+n1+n2 : 24.357541899441
Parent Value : 6.2569832402235
n0 : 2.0111731843575
n1 : 0.22346368715084
n2 : 0.44692737430168
n0+n1+n2 : 6.2569832402235
Parent Value : 10.055865921788
n0 : 1.1173184357542
n1 : 6.4804469273743
n2 : 0.22346368715084
n0+n1+n2 : 10.055865921788
Parent Value : 0
n0 : 1.7877094972067
n1 : 1.340782122905
n2 : 1.7877094972067
n0+n1+n2 : 0
Parent Value : 1.340782122905
n0 : 0.22346368715084
n1 : 1.5642458100559
n2 : 1.7877094972067
n0+n1+n2 : 1.340782122905
Parent Value : 12.513966480447
n0 : 8.7150837988827
n1 : 0.22346368715084
n2 : 0.89385474860335
n0+n1+n2 : 12.513966480447
Parent Value : 7.5977653631285
n0 : 4.2458100558659
n1 : 0.22346368715084
n2 : 1.340782122905
n0+n1+n2 : 7.5977653631285
Parent Value : 2.6815642458101
n0 : 0.22346368715084
n1 : 2.0111731843575
n2 : 1.7877094972067
n0+n1+n2 : 2.6815642458101
Parent Value : 14.525139664804
n0 : 4.2458100558659
n1 : 6.4804469273743
n2 : 1.1173184357542
n0+n1+n2 : 14.525139664804
Parent Value : 9.608938547486
n0 : 1.5642458100559
n1 : 3.1284916201117
n2 : 3.1284916201117
n0+n1+n2 : 9.608938547486
你想要什麼預期的結果?以陣列格式向我們展示 –
@AlivetoDie:我希望'$ n0 + $ n1 + $ n2'的值等於'$ n_value' –
@AlivetoDie如果可能的話,我也不想讓最小的值爲0但'$ n_value'大於0 –