mercredi 19 avril 2017

Computation inside foreach loop

I have a problem within the loop with correct computation.Right below is the original array which is to flipped.See result of second dd of the array. As you can see,the value is already sorted.Here comes the problem inside the foreach loop.My logic seems confusing even if my expected result is simple. See the following results below.Hope anyone could help me figuring it out the most easy way just come up the expected output.

array:5 [▼
   "1.4" => 40 
   "1.4" => 42 
   "1.5" => 38 
   "1.5" => 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++;
    }

result of flipped array of index and value

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;

    }

This is value of scholarship_points_two = 25

This is supposed to be the result:

id    criteria_gpa
40          25.00
42          25.00
38          22.50
39          22.50
41          20.00

The current result which were saved in the database (but wrong computation)

id    criteria_gpa
40          22.50
42          17.50
38          12.50
39          7.50
41          2.50



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire