mardi 18 avril 2017

Ranking retrieved data using loop in laravel

I have a collection of data $all_scholar_grade that hold values of grades.These grades in the second collection are not arrange from the highest order.This cant be done in ascending order because it is not a query.How do i arrange it or rank from the highest to lowest using loop or foreach? I'd tried figuring it out but i cant get the logic.Hope anyone could help me here.

    $all_scholar_id = array();
    $all_scholar_grade = array();
    $count = 0;
    foreach ($card_id as $id) {

        $card = ScholarCard::find($id);
        if ($card->scholar_grade_level == 12) {
            if ($card->scholar_GPA == 100 || $card->scholar_GPA == 99 || $card->scholar_GPA == 98 || $card->scholar_GPA == 97 || $card->scholar_GPA == 96 || $card->scholar_GPA == 95 ) {
             $card_gpa = 1.0;
            }elseif ($card->scholar_GPA == 94) {
                $card_gpa = 1.1;
            }elseif ($card->scholar_GPA == 93) {
                $card_gpa = 1.2;
            }elseif ($card->scholar_GPA == 92) {
                $card_gpa = 1.3;
            }elseif ($card->scholar_GPA == 91) {
                $card_gpa = 1.4;
            }elseif ($card->scholar_GPA == 90) {
                $card_gpa = 1.5;
            }elseif ($card->scholar_GPA == 89) {
                $card_gpa = 1.6;
            }elseif ($card->scholar_GPA == 88) {
                $card_gpa = 1.7;
            }elseif ($card->scholar_GPA == 87) {
                $card_gpa = 1.8;
            }elseif ($card->scholar_GPA == 86) {
                $card_gpa = 1.9;
            }elseif ($card->scholar_GPA == 85) {
                $card_gpa = 2.0;
            }elseif ($card->scholar_GPA == 84) {
                $card_gpa = 2.1;
            }elseif ($card->scholar_GPA == 83) {
                $card_gpa = 2.2;
            }elseif ($card->scholar_GPA == 82) {
                $card_gpa = 2.3;
            }elseif ($card->scholar_GPA == 81) {
                $card_gpa = 2.4;
            }elseif ($card->scholar_GPA == 80) {
                $card_gpa = 2.5;
            }elseif ($card->scholar_GPA == 79) {
                $card_gpa = 2.6;
            }elseif ($card->scholar_GPA == 78) {
                $card_gpa = 2.7;
            }elseif ($card->scholar_GPA == 77) {
                $card_gpa = 2.8;
            }elseif ($card->scholar_GPA == 76) {
                $card_gpa = 2.9;
            }elseif ($card->scholar_GPA == 75) {
                $card_gpa = 3.0;
            }elseif ($card->scholar_GPA <= 74) {
                $card_gpa = 5.0;
            }
        }else{
            $card_gpa = $card->scholar_GPA;
        }
        if ($card_gpa >= $scholarship->scholarship_gpa_to || $card_gpa <= $scholarship->scholarship_gpa_from) {
            $all_scholar_grade[] = $card_gpa;

            $all_scholar_id[] = $card->scholar_id;
        }

        $count++;         
    }

Output of my dd($all_scholar_id, $all_scholar_grade)

array:3 [▼
  0 => 29
  1 => 34
  2 => 35
]

array:3 [▼
  0 => 1.5
  1 => 2.1
  2 => 1.3
]   



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire