我在循环中遇到了正确计算的问题。右下方是要翻转的原始数组。查看数组的第二个dd的结果。 正如你所看到的,这个值已经被排序了。这就是foreach循环中出现的问题。即使我的预期结果很简单,我的逻辑似乎也很混乱。 请看下面的结果。希望有人能帮助我找出最简单的方法来获得预期的输出。
我试图更改密钥1.401和1.501,以避免与索引的迭代冲突。
array:5 [▼
"1.4" => 40
"1.401" => 42
"1.5" => 38
"1.501" => 39
"1.6" => 41
]
$count1 = 0;
foreach ($scho as $scholar_id2) {
$scholar = Scholar::find($scholar_id2);
$crits = New Criteria;
$crits->scholar_id = $scholar->scholar_id;
$crits->scholarship_id = $scholarship_id;
$crits->save();
$count1++;
}
索引和值的翻转数组的结果
array:5 [▼
"40" => 1.4
"42" => 1.4
"38" => 1.5
"39" => 1.5
"41" => 1.6
]
$list1 = Criteria::where('scholarship_id','=',$scholarship->scholarship_id)->get();
$all_id = [];
foreach ($list1 as $id) {
array_push($all_id, $id->criteria_id);
}
$last_scholar_gpa = 0;
$percentage = 1.2;
foreach ($all_id as $id2) {
$crits = Criteria::find($id2);
foreach ($all_list_GPA as $scholar_GPA) {
if($percentage > 0.5 && $last_scholar_gpa != $scholar_GPA)
$percentage = $percentage - 0.1;
$total_points_two = $scholarship->scholarship_points_two * $percentage;
$crits->criteria_gpa = $total_points_two;
}
$crits->save();
$last_scholar_gpa = $scholar_GPA;
}
这是scholarship_points_two = 25
的值这应该是结果:
id criteria_gpa
40 25.00
42 25.00
38 22.50
39 22.50
41 20.00
保存在数据库中的当前结果(但计算错误)
id criteria_gpa
40 22.50
42 17.50
38 12.50
39 7.50
41 2.50