Calculate Rank of Array Values in PHP

June 14, 2022
PHP

Sometimes we may need to calculate rank from an array of some elements like student mark etc. Here is how to calculate rank from array in PHP easily.

Calculate Rank of Array Values in PHP

 

Calculate Rank From Key => Value Array


//student mark array
$student_mark = [
    'REG_10001' => 89,
    'REG_10002' => 95,
    'REG_10003' => 35,
    'REG_10004' => 86,
    'REG_10005' => 12,
    'REG_10006' => 0,
    'REG_10007' => 0,
    'REG_10008' => 75,
    'REG_10009' => 86,
    'REG_10010' => 35,
];

$student_rank = calculate_rank($student_mark);

//calculate rank for key value pair array
function calculate_rank($rank_values): array {
    $rank_array = [];
    $rank = 0;
    $r_last = null;
    foreach ($rank_values as $key => $value) {
        if ($value != $r_last) {
            if($value > 0){ //if you want to set zero rank for values zero
            	$rank++;
            }
            $r_last = $value;
        }

        $rank_array[$key] = $value > 0 ? $rank: 0; //if you want to set zero rank for values zero
    }
    
    return $rank_array;
}

/**
OUTPUT
*/
Array
(
    [REG_10001] => 1
    [REG_10002] => 2
    [REG_10003] => 3
    [REG_10004] => 4
    [REG_10005] => 5
    [REG_10006] => 0
    [REG_10007] => 0
    [REG_10008] => 7
    [REG_10009] => 8
    [REG_10010] => 9
)

Calculate Rank From Multi Dimensional Array


//student mark array
$student_mark = [
    [
        'reg_no'       => 'REG_10001',
        'student_name' => 'Rita Book',
        'mark'         => '89',
    ],[
        'reg_no'       => 'REG_10002',
        'student_name' => 'A. Mused',
        'mark'         => '95',
    ],[
        'reg_no'       => 'REG_10003',
        'student_name' => 'Rose Bush',
        'mark'         => '35',
    ],[
        'reg_no'       => 'REG_10004',
        'student_name' => 'Greg Arias',
        'mark'         => '86',
    ],[
        'reg_no'       => 'REG_10005',
        'student_name' => 'Skye Blue',
        'mark'         => '12',
    ],[
        'reg_no'       => 'REG_10006',
        'student_name' => 'Don Messwidme',
        'mark'         => '0',
    ],[
        'reg_no'       => 'REG_10007',
        'student_name' => 'Emma Grate',
        'mark'         => '0',
    ],[
        'reg_no'       => 'REG_10008',
        'student_name' => 'Sarah Moanees',
        'mark'         => '75',
    ],[
        'reg_no'       => 'REG_10009',
        'student_name' => 'Mal Nurrisht',
        'mark'         => '86',
    ],[
        'reg_no'       => 'REG_10010',
        'student_name' => 'Stanley Knife',
        'mark'         => '35',
    ],
];

$student_rank = calculate_rank($student_mark);

//calculate rank for multi dimensional array
function calculate_rank($rank_values): array {
    $rank = 0;
    $r_last = null;
    foreach ($rank_values as $key => $arr) {
        if ($arr['mark'] != $r_last) {
            if($arr['mark'] > 0){ //if you want to set zero rank for values zero
                $rank++;
            }
            $r_last = $arr['mark'];
        }

        $rank_values[$key]['rank'] = $arr['mark'] > 0 ? $rank: 0; //if you want to set zero rank for values zero
    }
    return $rank_values;
}

//output
Array
(
    [0] => Array
        (
            [reg_no] => REG_10001
            [student_name] => Rita Book
            [mark] => 89
            [rank] => 1
        )

    [1] => Array
        (
            [reg_no] => REG_10002
            [student_name] => A. Mused
            [mark] => 95
            [rank] => 2
        )

    [2] => Array
        (
            [reg_no] => REG_10003
            [student_name] => Rose Bush
            [mark] => 35
            [rank] => 3
        )

    [3] => Array
        (
            [reg_no] => REG_10004
            [student_name] => Greg Arias
            [mark] => 86
            [rank] => 4
        )

    [4] => Array
        (
            [reg_no] => REG_10005
            [student_name] => Skye Blue
            [mark] => 12
            [rank] => 5
        )

    [5] => Array
        (
            [reg_no] => REG_10006
            [student_name] => Don Messwidme
            [mark] => 0
            [rank] => 0
        )

    [6] => Array
        (
            [reg_no] => REG_10007
            [student_name] => Emma Grate
            [mark] => 0
            [rank] => 0
        )

    [7] => Array
        (
            [reg_no] => REG_10008
            [student_name] => Sarah Moanees
            [mark] => 75
            [rank] => 6
        )

    [8] => Array
        (
            [reg_no] => REG_10009
            [student_name] => Mal Nurrisht
            [mark] => 86
            [rank] => 7
        )

    [9] => Array
        (
            [reg_no] => REG_10010
            [student_name] => Stanley Knife
            [mark] => 35
            [rank] => 8
        )

)

About Author
Adeeb C (233 Articles)

Adeeb C is a Web Designer, Web Application Developer, Android App Developer, WordPress Developer, Professional Blogger. He has 5 years of experience in WordPress, Web Developing, Professional Blogging, and 2 years of experience in Android App Developing.