我在选择最佳员工职位时遇到一些问题。
情况就是这样:
我选择了4名员工(E1
,E2
,E3
,E4
)及其在3个人群中:
* Random array ( Population I )= (
[0] = E1 => 200,
[1] = E2 => 155,
[2] = E3 => 130,
[3] = E4 => 98
)
* Random array ( Population II )= (
[0] = E2 => 155,
[1] = E3 => 130,
[2] = E1 => 200,
[3] = E4 => 98
)
* Random array ( Population III )= (
[0] = E4 => 98,
[1] = E1 => 200,
[2] = E3 => 130,
[3] = E2 => 155
)
然后,我想将该分数输入此功能:
f = ( N * score[0] ) + ( (N-1) * score[1] ) + score[2] + score[3] / N)
注意:N
是所选员工的数量。
健身功能示例(手动计算):
Population I : (4*200) + ((4-1)*155) + 130 + 98 / 4 = 373,25
Population II : (4*155) + ((4-1)*130) + 200 + 98 / 4 = 327
Population III : (4*98) + ((4-1)*200) + 130 + 155 / 4 = 319,25
那么如何使用PHP代码实现手动计算?
有人能帮助我吗?我现在试了一个星期,但仍然没有运气:(
答案 0 :(得分:0)
Here's an example implementation你的公式。但是,你的问题有两个明显的问题,所以让我们先解决这些问题:
无论如何,这是一个实施:
function doMyCalculation($selections) {
// Get number of selected employees
$num_selected = count($selections);
// Break up the format of your array - what is it supposed to be?
array_walk($selections, function(&$employee) {
list($emp, $val) = explode('=>', $employee);
$employee = (int) $val;
});
// Initialize variable
$return = 0;
// Loop through all "employees"
for($i = 0; $i < $num_selected; $i++) {
// For the first two, we're going to use N as a multiplier
if($i < 2)
// Use [N - current] as the multiplier (only twice)
$return += ($num_selected - $i) * $selections[$i];
else
// Otherwise, just add it normally
$return += $selections[$i];
}
// Divide the whole lot by N
$return /= $num_selected;
return $return;
}
echo doMyCalculation($arr1); // 373.25
echo doMyCalculation($arr2); // 327
echo doMyCalculation($arr3); // 319.25
如果您选择四个以上或少于四个员工等,您应该考虑以上几点来确定如何扩展这一点。如果没有这方面的知识,很难提供准确的答案。